Parameter Overloading


Value Parameters

A parameter declared with no modifier is a value parameter. A value parameter corresponds to a local variable that gets its initial value from the corresponding argument supplied in the method invocation. 

When a formal parameter is a value parameter, the corresponding argument in a method invocation must be an expression of a type that is implicitly convertible to the formal parameter type.

A method is permitted to assign new values to a value parameter. Such assignments only affect the local storage location represented by the value parameter – they have no effect on the actual argument given in the method invocation.


Reference Parameters

 

A parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a reference parameter does not create a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the method invocation.

When a formal parameter is a reference parameter, the corresponding argument in a method invocation must consist of the keyword ref followed by a variable reference of the same type as the formal parameter. A variable must be definitely assigned before it can be passed as a reference parameter.

 


Output Parameters 

 

A parameter declared with an out modifier is an output parameter. Similar to a reference parameter, an output parameter does not create a new storage location. Instead, an output parameter represents the same storage location as the variable given as the argument in the method invocation.

When a formal parameter is an output parameter, the corresponding argument in a method invocation must consist of the keyword out followed by a variable-reference of the same type as the formal parameter. A variable need not be definitely assigned before it can be passed as an output parameter, but following an invocation where a variable was passed as an output parameter, the variable is considered definitely assigned.

Within a method, just like a local variable, an output parameter is initially considered unassigned and must be definitely assigned before its value is used.

Every output parameter of a method must be definitely assigned before the method returns. Output parameters are typically used in methods that produce multiple return values. 


Parameter Arrays 

A parameter declared with a params modifier is a parameter array. If a formal parameter list includes a parameter array, it must be the last parameter in the list and it must be of a single-dimensional array type. For example, the types string[] and string[][] can be used as the type of a parameter array, but the type string[,] can not. It is not possible to combine the params modifier with the ref and out modifiers. The argument given for a parameter array can be a single expression of a type that is implicitly convertible to the parameter array type. Here it acts like a value parameter.

Alternatively, the invocation can specify zero or more arguments for the parameter array, where each argument is an expression of a type that is implicitly convertible to the element type of the parameter array. In this case, the invocation creates an instance of the parameter array type with a length corresponding to the number of arguments, initializes the elements of the array instance with the given argument values, and uses the newly created array instance as the actual argument.

Methods too are static and instance methods. A static method does not operate on a specific instance, and it is an error to refer to this in a static method. It is furthermore an error to include a virtual, abstract, or override modifier on a static method. An instance method operates on an instance of a class.

Here’s an example illustrating all the types of parameters used along with method overloading.

public class Class1
{

static void Main ( string [ ] args )
{

int x = 10, y = 20, z = 30 ;
Func ( x, y ) ;

Console.WriteLine ( x + " " + y ) ;
Func ( ref x, ref y ) ; 
Console.WriteLine ( x + " " + y ) ;
Func ( x, y, z ) ;
Console.WriteLine ( x + " " + y + " " + z ) ;

}

public static void Func( int a, int b)
{

a++ ;
b++ ;

}

public static void Func( ref int a, ref int b)
{

a++ ;
b++ ;

}

public static void Func( params int[] args)
{

foreach (int i in args)

i++ ;

}

}

The output is as follows:

10 20
11 21
11 21 30

Explanation:

Here we have overloaded the Func( ) function. The first call to Func( ) calls the first function defined. Here the parameters are value parameters and after execution we see that in Main( ) the change is not reflected.

On the second call, the second function is invoked where parameters are reference parameters. Whatever changes are made in the function are reflected in Main( ).

In the last function, we have used a parameter array, so the variables x, y and z are passed in the array. The changes are not reflected in Main( ).

A function cannot define overloaded methods, which differ only on ref and out.

So to check out what happens when we use the out keyword, comment out the function call and declaration of the function using the ref keyword and add the following in Main( )

int p, q ;
Func ( out p, out q ) ;
Console.WriteLine ( p + " " + q ) ;
and define a function like this:
public static void Func( out int a, out int b )
{

a = 88 ;
b = 77 ;

}

The output becomes:

10 20
88 77
10 20 30