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.
2. After that, add the following code on the click events of the buttons
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 :
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.