Search This Blog

Monday 21 December 2015

Types of Parameters in C#


Value
Ref
Out
Params

Value 

    class ValueType
    {
        public void Add(int num)
        {
            num++;
            Console.WriteLine("The value inside the method: {0}", num);
        }

        static void Main(string[] args)
        {
            int num = 10;

            Console.WriteLine("The value before calling the method: {0}", num);

            ValueType obj = new ValueType();

            obj.Add(num);  // Passing the variable by Value.

            Console.WriteLine("The value after calling the method: {0}", num);

            Console.ReadLine();
        }
    }

Output:
    The value before calling the method: 10
    The value inside the method: 11
    The value after calling the method: 10

Note:
     1. Value types directly contain values.
     2. Assigning one value type variable to another copies the contained value.
     3. A Value Type stores its contents in memory allocated on the stack.
     4. Value types can be created at compile time and Stored in stack memory.
     5. A value type parameter must first be initialized before being passed from the calling function to the called function


Ref

    class ReferenceType
    {
        public void Add(ref int num)
        {
            num++;
            Console.WriteLine("The value inside the method: {0}", num);
        }

        static void Main(string[] args)
        {
            int num = 10;

            Console.WriteLine("The value before calling the method: {0}", num);

            ReferenceType obj = new ReferenceType();

            obj.Add(ref num);  // Passing the variable by Reference.

            Console.WriteLine("The value after calling the method: {0}", num);

            Console.ReadLine();
        }
    }

Output:
    The value before calling the method: 10
    The value inside the method: 11
    The value after calling the method: 11

Note:
     1. The ref keyword causes an argument to be passed by reference, not by value.
     2. A Reference Type stores its contents in memory allocated on the heap.
     4. A ref parameter must first be initialized before being passed from the calling function to the called function.
     5. The effect of passing by reference is that any change to the parameter in the called method is reflected in the calling method


Out

    class OutType
    {
        public void Add(out int num)
        {
            num = 20;
        }

        static void Main(string[] args)
        {
            int num;

            OutType obj = new OutType();

            obj.Add(out num);  // Passing the variable by Out.

            Console.WriteLine("The value after calling the method: {0}", num);

            Console.ReadLine();
        }
    }

Output:
    The value after calling the method: 20

Note:
    1. The out parameter must be assigned to before control leaves the current method.
    2. Out parameter used for when we want to method return more than one value.


Params

    class ParamType
    {
        public int Add(params int[] num)
        {
            int values = 0;

            foreach (int val in num)
            {
                values = values + val;
            }

            return values;
        }

        static void Main(string[] args)
        {
            ParamType obj = new ParamType();

            int[] num = { 1, 2, 3 };

            Console.WriteLine("The value after calling the method: {0}", obj.Add(num)); // Passing the variable by params.

            Console.ReadLine();
        }
    }

Output:
    The value after calling the method: 6

Note:
    1. The parameter (params) array must be a single dimensional array
    2. A parameter (params) array must be the last parameter in a formal parameter list.

No comments:

Post a Comment