C# -

In C#, arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment. To pass a parameter by reference, use the ref or out keyword.

Passing Value Type Parameters

Passing Value Type Parameters :A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable.

    class PassingValByVal
        {
            static void SquareIt(int x)
            // The parameter x is passed by value.
            // Changes to x will not affect the original value of x.
            {
                x *= x;
                System.Console.WriteLine("The value inside the method: {0}", x);
            }
            static void Main()
            {
                int n = 5;
                System.Console.WriteLine("The value before calling the method: {0}", n);
                SquareIt(n);  // Passing the variable by value.
                System.Console.WriteLine("The value after calling the method: {0}", n);
                // Keep the console window open in debug mode.
                System.Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
            }
        }
        /* Output:
            The value before calling the method: 5
            The value inside the method: 25
            The value after calling the method: 5
        */

Passing Reference-Type Parameters

Passing Reference-Type Parameters :A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword.

    class PassingValByRef
        {
            static void SquareIt(ref int x)
            // The parameter x is passed by reference.
            // Changes to x will affect the original value of x.
            {
                x *= x;
                System.Console.WriteLine("The value inside the method: {0}", x);
            }
            static void Main()
            {
                int n = 5;
                System.Console.WriteLine("The value before calling the method: {0}", n);
                SquareIt(ref n);  // Passing the variable by reference.
                System.Console.WriteLine("The value after calling the method: {0}", n);
                // Keep the console window open in debug mode.
                System.Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
            }
        }
        /* Output:
            The value before calling the method: 5
            The value inside the method: 25
            The value after calling the method: 25
        */

Swapping Value by Reference

            static void SwapByRef(ref int x, ref int y)
            {
                int temp = x;
                x = y;
                y = temp;
            }
            static void Main()
            {
                int i = 2, j = 3;
                System.Console.WriteLine("i = {0}  j = {1}" , i, j);
                SwapByRef (ref i, ref j);
                System.Console.WriteLine("i = {0}  j = {1}" , i, j);
                // Keep the console window open in debug mode.
                System.Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
            }
            /* Output:
                i = 2  j = 3
                i = 3  j = 2
            */