Saturday, February 4, 2012

Method Parameters (• params • ref • out )


Method Parameters 
If a parameter is declared for a method without ref or out, the parameter can have a value associated with it. That value can be changed in the method, but the changed value will not be retained when control passes back to the calling procedure. By using a method parameter keyword, you can change this behavior.
Keywords you can use when declaring method parameters:
  • params keyword
  • ref keyword
  • out keyword  
·   params keyword 
o   By params keyword variable number of arguments for methods
o   You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type
o   You also can send no arguments
o    Only one params keyword is permitted in a method declaration

Example
Consider the following method:
public static void IntParams(params int[] items)
        {
            if (items.Length == 0)
            {
                Console.WriteLine("Empty parameters supplied!");
              return;
            }

            foreach (int i in items)
            {
                Console.Write("\t" + i);
            }

            Console.WriteLine();
        }

               
Calling the method:
                MyClass.IntParams(1, 2, 3, 4, 6, 7, 8, 9, 10);
Output:   1  2  3  4  6  7  8  9  10

MyClass.IntParams();
Output:   Empty parameters supplied!


·   ref keyword 
o   The ref  keyword causes an argument to be passed by reference, not by value
o   The effect of ref  keyword is that any change to the parameter is also reflected back in the argument variable of calling method i.e. both parameter and argument have same value
o   The ref  keyword must be explicitly used by calling method and in method definition
o   An argument that is passed to a ref  parameter must be initialized before it is passed (Note: for out keyword this is not necessary)
o   For Method overloading the usage of ref  and out keyword in a overload method is a compile error
The following method declaration gives compile time error:
//Error 1: Cannot define overloaded method 'OverloadMethod' because it differs from another method only on ref and out
public void OverloadMethod(out int i) { }
 public void OverloadMethod(ref int i) { }

Whereas the following method overloading works fine for ref and out:
public void OverloadMethod(int i) { }
 public void OverloadMethod(ref int i) { }

public void OverloadMethod(int i) { }
 public void OverloadMethod(out int i) { }

o  The ref  and out keywords cause different run-time behavior, they are not considered part of the method signature at compile time
o  The most important point is that “A method parameter can be modified by ref  regardless of whether it is a value type or a reference type. Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same.”
        public static void RefParamUsage(ref int i)
        {
           i = i + 44;
        }

  int val = 1;
         MyClass.RefParamUsage(ref val);
         Console.WriteLine(val);
  The output is :  i = 45
o  Properties are not variables. They are methods, and cannot be passed to ref  parameters.
  
·   out keyword 
o   The out keyword causes arguments to be passed by reference. Same as ref keyword, except that ref requires that the variable be initialized before it is passed. Whereas the variable as out argument doesn’t need to be initialized before it is passed.
o   The out contextual keyword is used in two contexts:
§  As a parameter modifier in parameter lists
§  In generic type parameter declarations in interfaces and delegates
o   The out keyword must be explicitly used by calling method and in method definition
o   The calling method must assign a value before the method returns
o   Declaring an out method is useful when you want a method to return multiple values.



No comments:

Post a Comment