a blog for those who code

Friday, 9 October 2015

Little about Out and Ref Parameter in C#

In this post we will be discussing about Out and Ref parameter in C#. It is one of those confusing topic of C# which beginers takes lot of time to understand. We will try to make it simple so that everyone will understand Out and Ref parameter in C#.

Out and Ref are keywords used in C# to get value in and out the function or in simple terms it helps you to define how data are passed from caller to callee and vice versa.

Before discussing about Out and Ref we should know how functions work in C#. Lets take a simple example -

static void Main(string[] args)
{
  var myMainVar = 30;
  myFunction(myMainVar);
  Console.WriteLine(myMainVar);
}
static int myFunction(int funcVariable)
{
  funcVariable += 20;
  return funcVariable;
}

In the above snippet we have a variable called myMainVar which I am passing to the function myFunction (having parameter funcVariable) thus value of myMainVar is copied to funcVariable. But myMainVar value will not change when the function is computed. This is because by default variables are passed "By Value" to methods and functions. Thus until and unless we copy the returned value again to myMainVar the data in myMainVar variable will not change as shown below. This actually means that any modification done in the callee will not get reflected to the caller.


Ref Parameter

When we have a parameter with ref keyword, instead of data, reference + data of the variable is passed to the function. Thus any modification of the variable in the callee will affect the variable to the caller.

static void Main(string[] args)
{
  var myMainVar = 30;
  myFunction(ref myMainVar);
  Console.WriteLine(myMainVar);
}
static void myFunction(ref int funcVariable)
{
  funcVariable += 20;
}

In the above snippet we have marked the variable using ref keyword thus reference and data of the variable is passed to the function. Since we are modifing the variable inside the function, the variable myMainVar will also get updated as shown below.


Out Parameter

When we have a parameter with out keyword, only reference of the variable is passed to the function. Its compulsory to initialize the variable before using it inside the function when you mark the parameter with out keyword. If you do not initialize the variable you wull get the error like "The out parameter 'funcVariable' must be assigned to before control leaves the current method".


static void Main(string[] args)
{
  var myMainVar = 30;
  myFunction(out myMainVar);
  Console.WriteLine(myMainVar);
}
static void myFunction(out int funcVariable)
{
  funcVariable = 0;
  funcVariable += 20;
}

In the above snippet we have marked the variable using out keyword thus only reference of the variable is passed to the function. We have to initialize the varaible inside the function and the modified value of the funcVariable (variable inside of function) is refelected to the myMainVariable (varaible in the caller) as shown below.


Difference between Ref and Out keyword is that in Ref data passes two way (from caller to callee and vice versa) and in Out data passes only one way from callee to caller.

When to use what ?

If you want your function to pass several outputs then use out keyword as C# function can return only one variable. Ref will be useful when you want to change the value of an immutable object (string), because you cannot change the value of a string (immutable object) once it has been created. Its not a good idea to use ref unless it is urgently needed.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License