Click here to Skip to main content
13,146,365 members (43,925 online)
Click here to Skip to main content
Add your own
alternative version

Stats

10.1K views
16 bookmarked
Posted 28 Sep 2015

Out and REF in C#

, 28 Sep 2015
Rate this:
Please Sign up or sign in to vote.
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.
 


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# youtube video which explain Out Vs Ref practically.

License

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

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
prashita gupta14-Oct-15 6:19
memberprashita gupta14-Oct-15 6:19 
GeneralRefernece added to my article Pin
Akhil Mittal 13-Oct-15 18:32
mvp Akhil Mittal 13-Oct-15 18:32 
GeneralRe: Refernece added to my article Pin
Shivprasad koirala13-Oct-15 18:39
mvpShivprasad koirala13-Oct-15 18:39 
GeneralMy vote of 5 Pin
Akhil Mittal 13-Oct-15 18:24
mvp Akhil Mittal 13-Oct-15 18:24 
QuestionGood article Pin
inacion30-Sep-15 23:10
memberinacion30-Sep-15 23:10 
GeneralMy vote of 4 Pin
Member 432084429-Sep-15 23:24
memberMember 432084429-Sep-15 23:24 
GeneralNice Article Pin
Amit Gupta AG29-Sep-15 6:53
memberAmit Gupta AG29-Sep-15 6:53 
QuestionGood Article Pin
Santhk29-Sep-15 4:35
professionalSanthk29-Sep-15 4:35 
QuestionMy thoughts Pin
Member 1168325129-Sep-15 2:13
memberMember 1168325129-Sep-15 2:13 
AnswerRe: My thoughts Pin
rone12329-Sep-15 13:06
memberrone12329-Sep-15 13:06 
AnswerRe: My thoughts Pin
inacion30-Sep-15 23:18
memberinacion30-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 28 Sep 2015
Article Copyright 2015 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid