Click here to Skip to main content
15,885,941 members
Articles / Programming Languages / C# 5.0

Out and REF in C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (24 votes)
28 Sep 2015CPOL3 min read 27.5K   17   13
This blog explains C# Out and REF parameters in detail.

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.
 

Image 1

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.
 
Image 2

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.
 
Image 3

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

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.
 
Image 5

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.
 
Image 6

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.
 
Image 7


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

Image 8

For Further reading do watch  the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
GeneralMy vote of 4 Pin
prashita gupta14-Oct-15 6:19
prashita gupta14-Oct-15 6:19 
GeneralRefernece added to my article Pin
Akhil Mittal13-Oct-15 18:32
professionalAkhil Mittal13-Oct-15 18:32 
GeneralRe: Refernece added to my article Pin
Shivprasad koirala13-Oct-15 18:39
Shivprasad koirala13-Oct-15 18:39 
Thanks sir i will add your article reference to mine. Thanks again.
My book .NET interview questions with 500 mostly asked questions in .NET world .NET Interview questions and answers

GeneralMy vote of 5 Pin
Akhil Mittal13-Oct-15 18:24
professionalAkhil Mittal13-Oct-15 18:24 
QuestionGood article Pin
Nelson Costa Inácio30-Sep-15 23:10
Nelson Costa Inácio30-Sep-15 23:10 
GeneralMy vote of 4 Pin
Member 432084429-Sep-15 23:24
Member 432084429-Sep-15 23:24 
GeneralNice Article Pin
Amit Gupta AG29-Sep-15 6:53
Amit Gupta AG29-Sep-15 6:53 
QuestionGood Article Pin
Santhakumar M29-Sep-15 4:35
professionalSanthakumar M29-Sep-15 4:35 
QuestionMy thoughts Pin
Member 1168325129-Sep-15 2:13
Member 1168325129-Sep-15 2:13 
AnswerRe: My thoughts Pin
rone12329-Sep-15 13:06
rone12329-Sep-15 13:06 
AnswerRe: My thoughts Pin
Nelson Costa Inácio30-Sep-15 23:18
Nelson Costa Inácio30-Sep-15 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.