Académique Documents
Professionnel Documents
Culture Documents
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.
Output:
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
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.
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.
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;
}
}
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());
}
Now once we run our program we will get the correct output as shown below:
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.