C# provides the
ref and the
out keyword to make arguments to be passed by reference. The semantic of the two is very similar. The only difference is in the initialization of the flaged variable:
refrequires the variable to be initialized before passing it to the function,
outrequires the variable to be initialized inside the function,
The use cases of these two keywords are also almost the same, and their too frequent usage is I beleive considered a code smell (although there are valid use cases like the
Because of this could someone explain, why there are two very similar tools in C# for so narrow use cases?
Also, on MSDN, it is stated that they have different run-time behavior:
Although the ref and out keywords cause different run-time behavior, they are not considered part of the method signature at compile time.
How is their run-time behavior different?
Both answers looks correct, thank you both. I accepted jmoreno's because it is more explicit.