Pages

Saturday, March 16, 2013

Difference between the ref and out keywords


What is “out” parameter?


The “out” parameter keyword on a method causes a it to refer the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in the variable when control passes back to the calling method.

In C#, a method can return only one value. If you like to return more than one value, you can use the out keyword. The out modifier return as return-by-reference. The simplest answer is that the keyword “out” is used to get the value from the method.


Always keep this in mind that:
  • You don't need to initialize the value in the calling function.
  • You must assign the value in the called function, otherwise the compiler will report an error.
For example just refer the below code, where the first method will result a compile time error because we did not assign any value to the out parameter that we passed to this method. In the second method, it will fill the result before exiting the method call. In the third example, it will return more than one result as output parameters.

 
public void CompileError(int a, int b, out int result)
{
    // this will throw compile time error as the out parameter is not 
    // assigned before function exit call.
}
 
public void Sum(int a, int b, out int result)
{
    // modify the result parameter before exit
    result = a + b;
}
 
public void Calculate(int a, int b, out int sum, out int multiply)
{
    // return multiple values using multiple out parameters
    sum = a + b;
    multiply = a * b;
}
 
 
// out parameter must be initialized before call
int result, sum, multiply;
 
Sum(5, 4, out result);
Calculate(5, 4, out sum, out multiply);

As shown in the above code snippet, always declare the out parameter before calling the respective method in your code. Once you call the method will return you the result in the out parameter.

What is “ref” parameter?

A parameter declared with a “ref” modifier is a reference parameter. Unlike a value parameter, a reference parameter will not create a new storage location instead it will represent the same storage location as the variable given as the argument in the method.
In C#, when you pass a value type such as int, float, double, structure etc. as an argument to the method parameter, it is passed by value. Therefore, if you modify the parameter value, it does not affect argument in the method call. But if you mark the parameter with “ref” keyword, it will reflect in the actual variable.     To pass by reference, use “ref” keyword. Therefore, if you modify the parameter value, it affects argument in the method call.
Always keep this in mind that:
    • You need to initialize the variable before you call the function.
    • It’s not mandatory to assign any value to the ref parameter in the method. If you don’t change the value, what is the need to mark it as “ref”?
For example, refer the below code snippet where the integer value will be passed as reference and inside the method the actual instance will be modified. The first call will throw compile time error because the value was not initialized before the call but the second call to this will work perfectly as we initialized the variable before passing it to the method.
 
public void Increment(ref int value)
{
    value++;
}
 
 
int myValue;
Increment(ref myValue); // return compile error as the ref parameter was not initialized
 
int myValue = 5;
Increment(ref myValue); // will execute properly
As shown in the above code snippet, always initialize the ref parameter before calling the respective method in the code. The rest will work just like out parameter but it is not mandatory to modify the variable inside the method (If you don’t change the value, what is the need to mark it as “ref”? Right?)