C Sharp Call By Reference

C# Call By Reference

To pass an argument as reference-type instead of the copy of the original value, i.e, to pass a reference of arguments to the function the ref keyword is used in C#. In the Function Call by Reference method of C#, the permanent changes are made in passed values which also modifies the original value of the variable.

Example:

using System;  
namespace Func_Example
{  
    class Example  
    {  
        public void Display(ref int a)  
        {  
             a += a;  
            Console.WriteLine("Value inside the function: "+ a);  
        }  
        static void Main(string[] args)  
        {  
            int a = 100;  
            Example xyz = new Example(); 
            Console.WriteLine("Value before calling: "+ a);  
            xyz.Display(ref a);            
            Console.WriteLine("Value after calling: " + a);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are passing the reference of arguments to the function during the function call.

Please Share