Monday, September 28, 2015

Out and REF in C#

Out and REF are one of those most confused topics in C#. In this small article we will try to simplify the same.

There are three important points to remember about OUT and REFand once you understand these three points you would never have problem with OUT and REF.

Point number 1:- By default variables are passed BYVAL to methods and functions. Out and Ref helps to pass variable BY REF (By reference) .
Let us try to understand the above point. Consider the below code snapshot where we have the “Main” method and its calling “SomeFunction” method and its passing “OutSideVar” variable to “SomeFunction”.

Now by default only value of “OutSideVar” will be copied to the method so any changes in “InsideVar” variable change will have no affect outside. You can see after the function call “OutSideVar” still has value “20”. In other words by default the values are passed BY VAL.


So if we summarize in a normal situation data is passed BY VAL , from the caller to callee. Any modifications done in the callee will not be reflected to the caller.


Point number 2:- When we mark parameters with REF, reference of the variable is passed to the function / method and any modification in that method and function will affect to the outside variable.
In the below code you can see we have marked variablesusing the REF keyword and you can see how the changes within the function modifies the outside variable also. This is happening because now variable is passed by reference.


Summarizing in this scenario DATA + REFERENCE is passed to the function and any modification inside the function affects the outside variable.


Point number 3:- When we mark parameters with OUT, ONLY reference of the variable is passed to the function or method and NOT DATA.Any modification in that method and function will affect outside variable as well. Also its compulsory to initialize the variable.

When we mark the variable as OUT only reference is passed no data is passed to the function. For instance in the below code the value “20” from “OutSideVar” is not sent to the function. But any modification in variable from the inside function is reflected to the outside variable.


Summarizing, you can term this also has one way but it’s one way from the callee to the caller and any data sent from the caller is discarded.


So summarizing what we have learnt:-
  • Out and Ref help to pass variables by reference.
  • In REF with reference data is also passed from caller to the callee and vice versa. It’s two way i.e. from caller to the callee and vice-versa.
  • In OUT only reference is passed and modified data from the callee is passed to the caller variable. This is one way from the callee to the caller.
  • In OUT if data is passed from the caller function it is discarded and the variable has to be initialized inside the function.
Below image summarizes what we talked in the above article.


Below is a nice C# video which explain Out Vs Ref.

No comments: