Differences between “out” and “ref” parameters in C#

Kavya Chowdary
3 min readNov 7, 2020

Both ref and out keyword are used to pass arguments by reference within a method or function. The calling method is the method that passes the parameters and calls another method which is referred to as the called method.

Ref

  • Ref keyword is used to pass a parameter which needs to be updated.
  • The parameter must be initialized before passing them using ref keyword.
  • It is not mandatory to assign a value to the parameter before passing it to the calling method.
  • Ref keyword can be used to pass a parameter from the calling method to called method and vice versa i.e., data can be passed bi-directionally. To put it simply, ref keyword can be used to send the data in and out of the called method.

Out

  • Out keyword is used when a called method needs to return more than one parameter.
  • It is not necessary to initialize the parameter before passing them using out keyword.
  • It is mandatory to assign a value to the parameter before passing it to the calling method.
  • Out keyword can only be used to pass data from the calling method to called method i.e., data can only be passed unidirectionally. In simpler terms, out keyword means data is going out of the called method.

Note:

  • Both ref and out are treated differently at run time, and they are treated the same at compile time.
  • Properties are not variables. Therefore it cannot be passed as an out or ref parameter.

Example:

Let’s look at some example code.

public void A()
{
int x;
int y;
int z;
B(x, ref y, out z); //Error
}

In the above code, we are trying to call method B from method A. Here, A is the calling method and B is the called method. As we are not initializing both the x and y parameters (y is the being passed by using ref keyword), the compiler throws an error “Use of unassigned local variable” for both x and y variables. But it does not throw any error for z variable because it is not mandatory to initialize a variable that is passed with an out parameter.

Now, let’s initialize the variables and call method B.

public void A()
{
int x = 1;
int y = 2;
int z = 3;
B(x, ref y, out z);
}

Another way of doing declaring variable z is as below:

public void A()
{
int x = 1;
int y = 2;
B(x, ref y, out int z);
}

Now let us take a look at method B.

public void B(int x, ref int y, out int z)
{
Console.WriteLine($"The value of x: {x}");
Console.WriteLine($"The value of y: {y}");
Console.WriteLine($"The value of z: {z}"); //Error: Use of unassigned out parameter 'z'
}

In the above code, we are trying to print the value of z, an out parameter, before assigning it a value. It throws an error saying “Use of unassigned out parameter ‘z’”. Note that even if we had assigned a value to z before passing it to the method B, it would still throw the error.

Let us now look at the entire code which does not throw any error.

public void A()
{
int x = 1;
int y = 2;
int z = 3;
B(x, ref y, out z);
}
public void B(int x, ref int y, out int z)
{
Console.WriteLine($"The value of x: {x}");
Console.WriteLine($"The value of y: {y}");

x = 10;
y = 20;
z = 30;
Console.WriteLine($"The value of x after updating: {x}");
Console.WriteLine($"The value of y after updating: {y}");
Console.WriteLine($"The value of z after updating: {z}");
}

Here, it is not mandatory to assign values to x and y variables.

When we call method A, the output will be:

The value of x: 1
The value of y: 2
The value of x after updating: 10
The value of y after updating: 20
The value of z after updating: 30

Originally posted on All Codes Lead To Rome.

--

--