There are three ways you can pass arguments to a function call: by value, using the ref keyword, and using the out keyword. This article explains the difference between Ref and Out Keywords in C#.

Default Method of Passing Arguments

When you pass an argument without specifying any additional instruction, it is called passing by value. In this case, the copy of an originally passed argument value is stored in a  function parameter and the original argument value remains unchanged. Here is an example of that:

class Program
    {
        static void Main(string[] args)
        {

            int a = 10;
            Console.WriteLine("Variable value before passing to function: " + a);
            PassByValue(a);
            Console.WriteLine("Variable value after passing to function: " + a);
            Console.ReadKey();
        }

        public static void PassByValue(int num)
        {
            num = num * 2;
            Console.WriteLine("Variable value inside the function: " + num);
        }

    }

You can see from the below output that the value of the variable “a” changes only inside the function and it remains the same before and after the function call.

Ref vs Out Csharp

Passing Arguments using the Ref Keyword

When you pass an argument to a function using the ref keyword. The reference or the memory location of the argument value is passed to the function being called. If the parameter that accepts the argument’s reference is updated. The change is reflected back to the originally passed argument as well. Let’s see this in action.

In the following example, the integer variable “a” contains the value 10. The variable “a” is passed as a function argument to the PassByRef() function where the reference of the variable “a” is stored inside the parameter “num”. The value of the “num” variable is updated to 20 (10 x 2).

Now if you print the value of the variable “a” before and after the function call, you will see that the value is updated.

class Program
    {
        static void Main(string[] args)
        {

            int a = 10;
            Console.WriteLine("Variable value before passing to function: " + a);
            PassByRef(ref a);
            Console.WriteLine("Variable value after passing to function: " + a);
            Console.ReadKey();
        }

        public static void PassByRef(ref int num)
        {
            num = num * 2;
            Console.WriteLine("Variable value inside the function: " + num);
        }

    }

Here is the output of the above script:

Ref vs Out C#

There is one limitation while passing the arguments using the ref keyword. To pass and update an argument value using the ref keyword, you first have to initialize the argument value. For instance, in the previous script, you first initialize the variable “a” and set it to 10. In case if you do not initialize the variable “a” you cannot call a function using the ref keyword. Here is an example. If you run the following script you will see an error:

class Program
    {
        static void Main(string[] args)
        {

            int a;

            PassByRef(ref a);
            Console.WriteLine("Variable value after passing to function: " + a);
            Console.ReadKey();
        }

        public static void PassByRef(ref int num)
        {
            num = 20;
            Console.WriteLine("Variable value inside the function: " + num);
        }

    }

The error below clearly tells that you are using an unassigned local variable “a”.

Understanding Ref vs Out C Sharp

Passing Arguments Using the Out Keyword

In many cases, you do not know the value of a variable before you pass it as an argument to a function. Rather, you want to initialize the variable value inside a function and want that the function returns the updated value. In such cases, you can use the out keyword. Here is an example.

In the script below, the variable “a” is not initialized. However, with the out keyword, you are able to pass it to a function without any error. Inside the function, the value of the variable “a” is set to 10 which is then printed on the console.

    class Program
    {
        static void Main(string[] args)
        {

            int a;

            PassByOut(out a);
            Console.WriteLine("Variable value after passing to function: " + a);
            Console.ReadKey();
        }

        public static void PassByOut(out int num)
        {
            num = 20;
            Console.WriteLine("Variable value inside the function: " + num);
        }

    }

Here is the output of the above script:

Learn Ref vs Out C Sharp

Conclusion

The main difference between the ref and out keyword lies in the initialization of the variables passed as arguments to a function call. With the ref keyword, you first have to initialize the argument, on the other hand with the out keyword, you can pass an uninitialized variable as an argument to a function call and then the function initializes the argument variable.