Вы находитесь на странице: 1из 7

Ref and Out Keywords in C#

Abstract
We all have hear in our old college days some lucky guys would have hear this
programming concept about passing variable by value and passing variable by
Reference in C or C++ in there school days. Value type calling methods contains the
data and the copy of the variables are created while in Reference Type the actual
address of the variable is passed to the variable.
Today I am going to talk about the Ref and Out Keywords in C# which changes the
default behavior of pass by reference in C# i.e. both Out and Ref keywords allows us
to pass variable by reference.

1. Calling method By Value


Normally we pass the value of the variable to the method using By value. Here
showing passing value to the function using By Value.
class Program
{
//properties
public int Number { get; set; }
public int result { get; set; }
static void Main(string[] args)
{
var obj = new Program();

Console.WriteLine("Enter the number");


obj.Number =Convert.ToInt32( Console.ReadLine());
obj.result = obj.Square(obj.Number);
Console.WriteLine("Program to calculate Square of a Num");
Console.WriteLine("The Square of {0} is {1}",obj.Number,obj.result);
Console.ReadLine();
}
/// <summary>
/// Square function Calucaltes square of the number
/// </summary>
/// <param name="num"> Accepts integer number</param>
/// <returns></returns>
private int Square(int num)
{
return num * num;
}

Output:

Figure 1: Call by Value Sample Program.

2. Out Keyword:
Out keyword is useful when we want to return more than 1 value from the function.
There are certain scenarios when we want to return multiple values, on these kind
of scenarios we can use Out Keyword.

Its necessary to use Out Keyword before variable name before Calling a
method
In Out Keyword its not necessary to initialize the variable which we we are
passing to the method, the called method variable is needed to be initialized
or assigned a value before the method performs the operations on the
variable.
Here we demonstrate Out Keyword

Figure 2: Demonstrating declaration of Out keyword required before the variable.

class Program
{
static void Main(string[] args)
{
int i;
Square(out i);
Console.WriteLine(i.ToString());
}
//method to calculate Square of A number
public static void Square(out int j)
{
j = j * j;
}
}
Once we try to build the program we face an error as shown below:

Figure 3: Error specifying that in Out Keyword we need to initialize the Called method
variable.

As we have discussed above we need initialize the called method variable before performing any
operation on the variable as shown below:
class Program
{
static void Main(string[] args)
{
int i;
Square(out i);
Console.WriteLine(i.ToString());
}
//method to calculate Square of A number
public static void Square(out int j)
{
//initialize the j variable
j = 10;
j = j * j;
}
}
Now if we try to build our program it will get compile. Lets run our program and check the Output
on the screen.

Figure 4: Output showing the Successful output of program.

So here we can note it down few notes for our self.

Data send by the Calling method is discarded in the Out Keyword and its
mandatory to initialize the variable inside the Called method.
Out is one way communication i.e. from Called to Calling Method.

3. Ref Keyword:
As the Keyword specifies in Ref keyword the reference of the variable is assigned to
the Called method variable. The benefit of using Ref keyword is that any change in
the parameter in the called method is reflected back to the main Calling method
variable.

To use a Ref keyword we must ref keyword on both the calling and called
method variable.
Variable needed to initialize at the calling method rather than called method
unlike Out Keyword.

Figure 5: Demonstrating declaration of ref keyword required before the variable.

class Program
{
static void Main(string[] args)
{
int i;
Square(ref i);
Console.WriteLine(i.ToString());
}
//method to calculate Square of A number
public static void Square(ref int j)
{
j = j * 10;
}
}

If we try to build this solution we will face an error as shown below:

Figure 6: Error specifying that in Ref Keyword we need to initialize the variable in the Calling
method Unlike Out.

class Program
{
static void Main(string[] args)
{
int i;
//initialize the variable
i = 10;
Square(ref i);
Console.WriteLine(i.ToString());
}

//method to calculate Square of A number


public static void Square(ref int j)
{
//no need to initialize at called method
j = j * 10;
}

Now once we run our program we will get the correct output as shown below:

Figure 7: Output showing the Successful output of ref program

We can sum the whole Ref and Out Keyword in below mentioned points
1. Both Out and Ref keyword allow us to pass value by Reference.
2. In Ref any change in the state of the variable will be reflected to both the side
i.e. Calling to the Called method or Called to Calling method variable.
3. In Out keyword the its is not mandatory to pass the value from the Calling
method as it will discarded at the Called method and it will only send the
Called method variable Out value.

If would recommend watching Ref vs. Out Video by clicking on below


image for full in depth.

Вам также может понравиться