Ref vs Out in C#


Recently I was reading an article relate to the Value type & Reference type variables. So I came across concept of Ref & Out keywords which can be use with the parameters of a method.

Not to be confused, the concept of Reference & Value types IS NOT same as passing parameters as Out or Ref.

I know everybody is aware about the Value & Reference type variables. So I will not go into the details of these.

Now to continue with our ref & out keywords, both the types allow the user to pass the parameter by reference. This means, by passing the parameter as reference, we are passing the reference of the actual value of the variable and NOT the value itself. So any change in the value of parameter will be reflected, after the function call is complete.

The parameters can be both the Value type or reference type. This implies that even if user passes an integer value by using a ref or out keyword, the change in value will be reflected after the function call is complete.

Let’s create an example :

1. Create a new website and add two labels, one textbox and three buttons on the Default.aspx page.

Textbox will be for getting the user input to test, labels will be to display the initial & final values and the buttons will be to change the value of a Value type(integer) by passing it using ref and out and one button to display the normal value type behaviour.

1

2. After that, add the following code on the click events of the buttons

2

The function ValueTypeByOut will be changing the values of an integer i.e. the value type using the out type & the ValueTypeByRef will be doing the same functionality by using the ref type. The third function NormalCall  will be simply changing the value of parameter passed. The functions are defined as below : 

3

3. Run the application and enter an integer value. Click on the buttons one by one and see the results. As in the example, I added the input value as 21. results

For all the three cases, the input value remains 21 but the output value changes according to the code in the functions called. For the first two cases uses the ref & out keywords to pass the value type data as reference type. But third function is simple example of any normal function that we call in our daily codes. So the functions in the first two cases are passing the reference of the variable i. So any change in its value is reflected even after the function call is complete.

So the basic question arises, when can we use the parameters as ref and out. So :

1. Use ref or out when we want to return more than one values from a function

Use out when we do not require or cannot initialize the variable. This is the basic difference between the two according to which ref type parameter needs to be initialized before being passed into the function.

2. Use out when we do not require or cannot initialize the variable. This is the basic difference between the two according to which ref type parameter needs to be initialized before being passed into the function.

3. Pass a value type data type as a reference type. This condition arises only due to some coding specific logic or requirement.

So these are the three points I think can be considered possible uses of these two keywords. If anyone is having any other better idea or use, then please do add your comment.

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in C# and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s