Skip to main content

Passing Parameters by Value and Reference with Example in C#

 Passing Parameters by Value and Reference with Example in C#

Passing a Value-Type parameter to a method by value means passing a copy of the variable to the method. So the changes made to the parameter inside of the called method will not have an effect on the original data stored in the argument variable.

 In C#, Value-Type variable contain the value directly on its memory and Reference-Type variable will contain a reference of its data.

 Passing Parameters by Value Example in C#

Following is the example of passing a value type parameter to a method by value in C#.

 

static void Main(string[] args)

        {

            int x = 10;

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

            Multiplication(x);

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

                      

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

 

        }

        public static void Multiplication(int y)

        {

            y *= y;

            Console.WriteLine($"Variable Value Inside the Method: {y}");

        }

  

From the above example, the variable x is a value type and its passed to the Multiplication method. The content of variable x copied to the parameter y and made required modifications in the Multiplication method but the changes made inside of the method have no effect on the original value of the variable.

 

Value Type

 


When we execute the above C# program, we will get the result as shown above.

 

 

From above result, the variable value not changed even after we made the modifications in our method.

 

Pass parameter by Reference (ref) 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 the called method will have an effect on the original data stored in the argument variable.

 

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

 

Declaration of C# Pass By Reference

The following is a simple example of passing parameters by reference in C#.

 

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

Multiplication(ref r);

 

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

 

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

 

Passing Parameters by Reference Example in C#

 

The code below is an example of passing a value type parameter to a method by reference in C#.

 

 static void Main(string[] args)

        {

            int x = 10;

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

            Multiplication(ref x);

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

                      

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

 

        }

        public static void Multiplication(ref int y)

        {

            y *= y;

            Console.WriteLine($"Variable Value Inside the Method: {y}");

        }

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

 

Reference Type


 

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

 

If we observe the above result, the changes that we did for the variable in the called method have reflected in the calling method also.


References

1.    Tutlane

2.    Tutorial Teachers

Comments