ref vs out in C# - cover image

ref vs out in C#

If you are a regular C# user, you must have used the ref & the out keyword. Ever wondered, what makes one different from one another?

There is a really slim difference between the two & we are going to cover these differences in this article.

ref keyword

The ref requires the value to be set before its usage. It needs the value to be declared first before passing the ref to a method.
The method accepting the ref parameter can read the actual value & can modify the value of the parameter. The modified value will be reflected in the original variable.

out keyword

The out keyword doesn’t imply at the definition of the variable before the usage. We can pass an unassigned variable with the out keyword
The method accepting out parameter must set the out value before leaving the method.

ref keyword usage

Consider the following method that accepts a ref param:

static void SomeMethod(ref int i)
{
    Console.WriteLine("Value received inside method :" + i);
    i += 10;
    Console.WriteLine("Updated value inside method :" + i);
}

To call the method, we must call it like:

SomeMethod(ref i);

The code will look like:

static void Main(string[] args)
{
    int i = 1;

    SomeMethod(ref i);
    Console.WriteLine("Updated value outside method :" + i);

    Console.ReadKey();
}

static void SomeMethod(ref int i)
{
    Console.WriteLine("Value received inside method :" + i);
    i += 10;
    Console.WriteLine("Updated value inside method :" + i);
}

Executing the main method gives the following output:

ref output
ref output

Changing the value inside the function reflected the change outside the function. This is what ref keyword is.

Passing uninitialized ref keyword

When trying to call the method with uninitialized int variable, we get the following error.

unassigned ref variable passed to method
Unassigned ref variable passed to a method

Hence, we need to initialize the ref parameter before passing it as the ref parameter.

out keyword usage

Consider the following method that accepts an out param:

static void SomeMethod(out int i)
{
    i = 1;

    Console.WriteLine("Value received inside method :" + i);
    i += 10;
    Console.WriteLine("Updated value inside method :" + i);
}

To call the method with out param, we must call it like:

SomeMethod(out i);

The actual code will look like:

static void Main(string[] args)
{
    int i;

    SomeMethod(out i);
    Console.WriteLine("Updated value outside method :" + i);

    Console.ReadKey();
}

static void SomeMethod(out int i)
{
    i = 1;

    Console.WriteLine("Value received inside method :" + i);
    i += 10;
    Console.WriteLine("Updated value inside method :" + i);
}

Executing the main method gives output as:

out output

Notice here, we didn’t initialize the variable before passing out param to SomeMethod. The method must need to assign (if not already assigned) some value using the out keyword.

Not modifying an out param

static void SomeMethod(out int i)
{
    Console.WriteLine("Not modifying variable i");
}

When I try to modify the SomeMethod to this code above, I get an error:

unassigned out error

Hence, we must assign an out variable a value before leaving the method.

Method overloading based on ref & out

void SomeMethod(ref int i)
{
    Console.WriteLine("With ref");
}

void SomeMethod(out int i)
{
    Console.WriteLine("With ref");
}

When trying to create two different overloads for a method based on ref & out keyword, we get an error “Program cannot define overloaded methods that differ only on ref and out.

ref vs out overloading methods

This means we are not allowed to overload methods based on ref and out keywords.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.