Saturday, May 8, 2021

C# Examples of out, ref and param keywords

ref Parameter Modifier:
The ref keyword is used to pass parameter by reference to a method. Thus, the ref is parameter modifier. The ref variable passes the memory reference of an initialized variable which is accessed by the called method ref variable. In the following example, the RefFunction(ref Var2); statement calls the method public static void RefFunction(ref int x)  { }. Both ref variables Var2 and x are referencing the same memory location. The ref variable must be initialized before passing it as parameter.
Example:
using System;
namespace ConsoleRef
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nExample of ref keyword:");
            int Var2 = 2;
            RefFunction(ref Var2);
            Console.WriteLine("Original value of Var2 is now modified.");
            Console.WriteLine("Var2: {0}", Var2); // 2
            Console.ReadKey();
        }
        public static void RefFunction(ref int x)
        {
            Console.WriteLine("Before processing, value of Var2 is {0}", x);

            x = x + 1;
            Console.WriteLine("Inside RefFunction, value of Var2 is changed to {0}", x);
        }
        
    }
}
OUTPUT
Example of ref keyword:
Before processing, value of Var2 is 2
Inside RefFunction, value of Var2 is changed to 3
Original value of Var2 is now modified.
Var2: 3
out Parameter Modifier:
Like a ref parameter, an out parameter is passed by reference but it need not be initialized unlike ref modifier.
Example:
using System;
namespace ConsoleOut
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nExample of out keyword:");
            int Var3; //not initialized, even if initialized, value of out variable depends upon processed value in
            int y = OutFunction(out Var3); // outFunction will return the value of Var3
            Console.WriteLine("Value of out Var3 by OutFunction: {0}", Var3); // 3
            Console.WriteLine("Value returned by OutFunction: {0}", y); // 4
            
            Console.ReadKey();
        }
        public static int OutFunction(out int x)
        {
            x = 2; //local variable
            x++; // value increased by 1
            return 4;
        }
    }
}
OUTPUT
Example of out keyword:
Value of out Var3 by OutFunction: 3
Value returned by OutFunction: 4
params Parameter Modifier:
The params parameter modifier is specified on the last parameter of a method so that the method accepts any number of arguments of a particular type. The parameter type must be declared as an array.
Example:
using System;
namespace ConsoleParam
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nExample of param keyword:");
            int Var4;
            ParamsFunction(out Var4, 2, 3, 4, 9);
            Console.WriteLine("Sum of 4 numbers: {0}", Var4);
            ParamsFunction(out Var4, 2, 3, 4, 5, 8, 9); // no. of parameters changed
            Console.WriteLine("Sum after no. of parameters changed");
            Console.WriteLine("Sum of 6 numbers: {0}", Var4);
            Console.ReadKey();
        }
        public static void ParamsFunction(out int res,

        params int[] input)
        {
            res = 0;
            foreach (int x in input) res += x;
        }
    }
}
OUTPUT
Example of param keyword:
Sum of 4 numbers: 18
Sum after no. of parameters changed
Sum of 6 numbers: 31
Pass by Value Example:
using System;
namespace ConsoleValue
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nExample of pass by value:");
            int Var1 = 1;
            ValueFunction(Var1);
            Console.WriteLine("Var1: {0}", Var1); // 1
            Console.ReadKey();
        }
        public static void ValueFunction(int x)
        {
            Console.WriteLine("Before processing value of Var1 is {0}", x);
            x = x + 1;
            Console.WriteLine("Inside ValueFunction, value of Var1 is changed to {0}", x);
            Console.WriteLine("But this change is local to the ValueFunction function.");
        }
        
    }
}
OUTPUT
Example of pass by value:
Before processing value of Var1 is 1
Inside ValueFunction, value of Var1 is changed to 2
But this change is local to the ValueFunction function.
Var1: 1

Consolidated Examples:
using System;
namespace ConsoleRefOutParam

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("\nExample of pass by value:");

            Console.WriteLine("-------------------------");

            int Var1 = 1;

            ValueFunction(Var1);

            //Var1 value remains intact, ValueFunction() processes Var1

            //and changes its value but original value remains the same

            Console.WriteLine("Original value of Var1 remains the same");

            Console.WriteLine("Var1: {0}", Var1); // 1

            Console.WriteLine("\nExample of ref keyword:");

            Console.WriteLine("-----------------------");

            int Var2 = 2;

            RefFunction(ref Var2);

            Console.WriteLine("Original value of Var2 is now modified.");

            Console.WriteLine("Var2: {0}", Var2); // 2

            Console.WriteLine("\nExample of out keyword:");

            Console.WriteLine("-----------------------");

            int Var3; //not initialized, even if initialized, value of out variable depends upon processed value in

            int y = OutFunction(out Var3); // outFunction will return the value of Var3

            Console.WriteLine("Value of out Var3 by OutFunction: {0}", Var3); // 3

            Console.WriteLine("Value returned by OutFunction: {0}", y); // 4

            Console.WriteLine("\nExample of param keyword:");

            Console.WriteLine("-------------------------");

            int Var4;

            ParamsFunction(out Var4, 2, 3, 4, 9);

            Console.WriteLine("Sum of 4 numbers: {0}", Var4);

            ParamsFunction(out Var4, 2, 3, 4, 5, 8, 9); // no. of parameters changed

            Console.WriteLine("Sum after no. of parameters changed");

            Console.WriteLine("Sum of 6 numbers: {0}", Var4);

            Console.ReadKey();

        }

        public static void ValueFunction(int x)

        {

            Console.WriteLine("Before processing value of Var1 is {0}", x);

            x = x + 1;

            Console.WriteLine("Inside ValueFunction, value of Var1 is changed to {0}", x);

            Console.WriteLine("But this change is local to the ValueFunction function.");

        }

        public static void RefFunction(ref int x)

        {

            Console.WriteLine("Before processing, value of Var2 is {0}", x);

            x = x + 1;

            Console.WriteLine("Inside RefFunction, value of Var2 is changed to {0}", x);

        }

        public static int OutFunction(out int x)

        {

            x = 2; //local variable

            x++; // value increased by 1

            return 4;

        }

        public static void ParamsFunction(out int res,

        params int[] input)

        {

            res = 0;

            foreach (int x in input) res += x;

        }

    }

}

OUTPUT

Example of pass by value:

-------------------------

Before processing value of Var1 is 1

Inside ValueFunction, value of Var1 is changed to 2

But this change is local to the ValueFunction function.

Original value of Var1 remains the same

Var1: 1

Example of ref keyword:

-----------------------

Before processing, value of Var2 is 2

Inside RefFunction, value of Var2 is changed to 3

Original value of Var2 is now modified.

Var2: 3

Example of out keyword:

-----------------------

Value of out Var3 by OutFunction: 3

Value returned by OutFunction: 4

Example of param keyword:

-------------------------

Sum of 4 numbers: 18

Sum after no. of parameters changed

Sum of 6 numbers: 31


No comments:

Post a Comment

Hot Topics