out
variable references can be initialised with the address of an unassigned variable but ref
variables can't do that, making out
better than ref
. Then what is the need for ref
?

- 4,964
- 2
- 25
- 43
-
1The correct answer is "because it is". That's the definition of `out`. – Mephy Oct 12 '14 at 05:05
-
@Mephy And still, 5 people gathered to provide an answer. – mihai Oct 12 '14 at 05:17
-
@Mephy no offens, can you please forward the link that define this behaviour of out parameter .for better understanding – Shamseer K Oct 12 '14 at 05:22
-
2@ShamseerKSmr It's a simple google `c# out` search. You'll get the MSDN page: http://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx. There, you'll see this comment: _"Although variables passed as out arguments do not have to be initialized before being passed, the called method is **required** to assign a value before the method returns"_. Simple! – mihai Oct 12 '14 at 05:24
-
1Can you please clarify what you are asking - you essentially said that you know that compiler must show error because it is specified in C# specification (based in the facet you said you know difference between `ref` and `out`). So are you looking for historical reason why C# specification have both `ref` and `out`? or something else? (Note that information you want may be present in [ref vs out](http://stackoverflow.com/questions/135234/difference-between-ref-and-out-parameters-in-net) SO question.) – Alexei Levenkov Oct 12 '14 at 05:34
-
@Mihai thanks : your link clarified my doubt – Shamseer K Oct 12 '14 at 05:39
-
@AlexeiLevenkov thanks doubt clarified – Shamseer K Oct 12 '14 at 05:41
-
1you are not supposed to pass initialized variables into an out param. that's not what it is for. so why would the compiler take that into account. – RadioSpace Oct 12 '14 at 05:45
4 Answers
As you said that you are aware about the difference and thus I am assuming that you are also aware of the fact that out arguments must be assigned.
Thus, I believe you want to know why an out argument need to assigned.
out keyword - it means that you are using this argument in order to output something.
consider, int add(int x, inty). If this method won't return anything should it work. Here also we are declaring a contract that this method returns integer. Similarly in case of out keyword we are defining a contract that out variable will be assigned and will be provided as output.
Why do we need to update? This is because we are explicitly marking it as an out argument and thus we are sure that we are going to assign a value and return it. If there is scenario where we do not have anything to assign and we are still using out argument then I am very sure we have a design problem.
ref keyword: - it means that you are passing reference. It never means that you will assign it and provide some output. You can use it in that way but it is not a contract. You have a reference of the argument and you could do anything with it. In C# arguments are passed by value by default. ref Keyword gives you a way to pass the arguments by reference.
You can get more information about ref keyword here.
Hope this will help.

- 980
- 11
- 20
The problem is because out parameters MUST be either initialized or assigned before the function can end. It's basically so you don't forget to set the variable.
static void Method(out int pointer)
{
pointer = 5; //Initialize or assign to fix the error
}

- 3,926
- 5
- 20
- 41
My understanding is that out parameters are set by the method being called, not passed into the method.
So these would be valid:
class Program
{
static void Main(string[] args)
{
int integer;
Method(out integer);
// integer is now 9
}
static void Method(out int pointer)
{
...
pointer = 9;
}
}
// Option 2:
class Program
{
static void Main(string[] args)
{
int integer = 9;
Method(integer);
}
static void Method(int pointer)
{
//Do something with pointer = 9
}
}

- 1,254
- 9
- 20
If you want to just pass the parameter to a function by reference, you should use ref
:
class Program
{
static void Main(string[] args)
{
int integer = 9;
Method(ref integer);
}
static void Method(ref int pointer)
{
}
}
This will compile normally.

- 6,726
- 5
- 24
- 43