+ 4
Ref vs Out
3 odpowiedzi
+ 5
ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.
So while ref is two-ways, out is out-only.
+ 2
There is one difference before that you should know one rule in C#: you can't pass a variable before initializing it which means setting a default value to it.
so the are two scenarios:
First you want to pass an object and you need the value of the variable being passed to it so you use ref keyword
Second you want to pass an object but you don't need to value of object being passed to it. in this case use out.
Note that the only way of passing a variable without it's value assigned is to use the out keyword otherwise there would be compiler error.
0
@Kassem Thanks for explanation :)