Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

23 June 2012

Method parameters in c#


In this article we will see different keywords which are used for method parameters.

Parameter passing ways to a method:
To a method the parameter passing types are
  1. Pass by value ( value parameter)
  2. Pass by reference(reference parameter)
  3. Output parameter (out parameter)
  4. Array parameter (param)
We will discuss individually each one with examples below.

Pass by Value: 
when the method is called values of actual parameters are assigned to formal parameters of method. Change in format parameter does not change value of value parameter.
Example:
   class PassByVlaueUsage
    {
        public static void Main()
        {
            int x = 10;
            update(10);
            Console.WriteLine("value of x is {}", x);
            Console.ReadLine();
        }
        public static void update(int y)
        {
            y = y + 1;
        }
    }
x is a actual parameter and y is formal parameter.
When the udpate method is called value of x is assigned to y and y is located at new memory location. So change in y does not affect value of x.
Output is:
 

Pass by reference: 
parameter which has ref modifier is called reference parameter. Reference parameter does not create a new storage location instead it uses the same storage location of actual parameter.
When formal parameter is declared as ref the corresponding actual parameter in calling method should be declared as ref.
Example
class PassByReferenceUsage
    {
        public static void Main()
        {
            int x = 10;
            update(ref x);
            Console.WriteLine("value of x is {0}", x);

            Console.ReadLine();
        }
        public static void update(ref int y)
        {
            y = y + 1;
        }
    }
x is a actual parameter and y is formal parameter.
When the update method is called value of x is assigned to y and y is located at same memory location. So change in y affects value of x.
Output is:


Output parameters:
which are used to pass result back to calling method. The out parameter is also does not create new location. When formal parameter is declared as out the corresponding actual parameter in calling method should be declared as out.
Example:
class OutParameterUsage
    {
        public static void Main()
        {
            int x = 10;
            int k=20;
            multiply(x,out k);
            Console.WriteLine("value of out variable k is {0}", k);

            Console.ReadLine();
        }
        public static void multiply(int x,out int y)
        {
            y = x * 5;
        }
    }
x is a actual parameter and y is out parameter.
When the multiply method is called value of x is multiplied with 5 and assigned to y. so the y value is passed back to calling method.
Output is:


Array Parameters:
A parameter declared with params modifier  is a parameter array. It should be one dimensional array.
Public void methodName(params int[] i)

Example:
    class ParameterArrayUsage
    {
        static void ParamArrayMethod(params int[] i)
        {
           Console.WriteLine("Passed parameter array Elements are :");
            foreach (int j in i)
            {
                Console.Write(" " + j);
            }
            Console.WriteLine();

        }
        public static void Main()
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            ParamArrayMethod(arr1);
            ParamArrayMethod();
            ParamArrayMethod(10, 11, 12);
            Console.ReadLine();
        }
    }
Calling like
            ParamArrayMethod(arr1);
            ParamArrayMethod();
            ParamArrayMethod(10, 11, 12);

Are equal.

Output:


Note : We cannot add params with ref or out keywords.


0 Comments:

Post a Comment