C# Pass By Reference (Ref) with Examples

  Keywords : C# parameters passing by reference method with examples, C# call by reference or ref parameter to pass a reference of variable to the method with examples

Here we will learn passing parameters by reference or call by reference in c# or ref parameter in c# with examples and how to use c# pass by reference or call by reference to pass a value type parameters by reference with examples.

C# Passing Parameters By Reference

In c#, passing a value type parameter to a method by reference means passing a reference of the variable to the method. So the changes made to the parameter inside of called method will have an effect on the original data stored in the argument variable.

 

By using ref keyword, we can pass a parameters reference-type and it’s mandatory to initialize the variable value before we pass it as an argument to the method in c# programming language.

 

As discussed earlier, value-type variables will contain the value directly on it memory and reference-type variables will contain a reference of its data.

Declaration of C# Pass By Reference

Following is simple example of passing parameters by reference in c# programming language.

 

int x = 10; // Variable need to be initialized

Multiplication(ref x);

If you observe above declaration, we declared and assigned a value to the variable x before we pass it as an argument to the method by using reference (ref).

 

To use ref parameter in c# application, both the method definition and the calling method must explicitly use the ref keyword.

C# Passing Parameters By Reference Example

Following is the example of passing a value type parameter to a method by reference in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 10;

            Console.WriteLine("Variable Value Before Calling the Method: {0}", x);

            Multiplication(ref x);

            Console.WriteLine("Variable Value After Calling the Method: {0}", x);

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

        public static void Multiplication(ref int a)

        {

            a *= a;

            Console.WriteLine("Variable Value Inside the Method: {0}", a);

        }

    }

}

If you observe above example, we are passing the reference of variable x to the variable a in Multiplication method by using ref keyword. In this case, the variable a contains the reference of variable x so the changes that made to the variable a will affect the value of variable x.

Output of C# Passing Parameters By Reference Example

When we execute above c# program, we will get the result like as shown below.

 

C# Passing Parameters By Reference Example Result

 

If you observe above result, the changes whatever we did for variable in called method has reflected in calling method also.

 

This is how we can pass parameters to the method by reference using ref keyword in c# programming language based on our requirements.