+ 4

Ref vs Out

26th Mar 2017, 3:49 PM
Kassem Shehady
Kassem Shehady - avatar
3 Antworten
+ 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.
26th Mar 2017, 3:49 PM
Kassem Shehady
Kassem Shehady - avatar
+ 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.
29th Apr 2017, 8:06 PM
Amir Pourmand
Amir Pourmand - avatar
0
@Kassem Thanks for explanation :)
12th Feb 2022, 7:28 PM
Sufian Ahmad
Sufian Ahmad - avatar