In C#, ref
and out
keyword.
How to it affect memory management? Is there any difference in memory management for ref
and out
keyword?
In C#, ref
and out
keyword.
How to it affect memory management? Is there any difference in memory management for ref
and out
keyword?
Even though the mechanism used behind the scene is the same, the difference between the two keywords is in what the compiler must verify about each parameter:
ref
keyword, the compiler checks that you have initialized it before making the callout
keyword, the compiler checks that the method that you call has made an assignment to the corresponding argument before exiting.This difference allows for the out var
construct, which has been added to C# 7.0. This feature would not be possible with ref
alone because of the initialization requirement.
There is no difference between the two as far as the memory management is concerned: in both cases the reference itself is passed by value, and the code using the reference is adding an extra level of dereference.