Params Array in C#

Worried about Overloading a method due to unpredictable inputs!!
Here is a solution to this provided by C# and explained by Ms. Asha Bora.

Param arrays are very useful if you want to write methods that can take variable numbers of arguments, possibly of different types as parameter. If any one is familiar with OOPS then, he must be grinding his teeth in frustration at this sentence…Since in OOPS you use overloaded methods for this problem.

Overloading is the technical term for declaring two or more methods with the same name in the same scope. Overloading handles a situation where type and number of parameters vary. Where as param array can handle the situation if the types of parameter do not vary. While declaring a method suppose you are not sure about the number of arguments passed as a parameter, then we can use the param array.

How to declare Params array:
class A
{
public static int Array_Min(params int [] paramList)
{
//code
}
}


The effect of params keyword is that it allows you to call it by using any number of integer arguments. For example to find the minimum of 2 integer values, we would write it as:
int min=A.Array_Min(1,2);

The compiler translates this call into code similar to this:
int []array=new int[2];
array [0]=1;
array [1]=2;
int min=A.Array_Min(array);
similarly to find the minimum of 3 integer values,we would write the code as: int min=A.Array_Min(1,2,3);


Points to be noted about params arrays:
· You can use params keyword on only one dimensional arrays.
· You can’t overload a method based solely on the params keyword. The params keyword does not form part if a method’s signature, for example:
//compile time error: duplication declaration
public static int Min(int[]paramList)…
public static int Min(params int[]paramList)…
· You are not allowed ref or out params arrays, for example:
//compile-time errors
Public static int Min(ref params int[]paramList)…
Public static int Min(out params int[]paramList)…
· A params array must be the last parameter i.e. You can have only one params array per method.

A params int array is very useful since it allows any number of int arguments in a method call. you can use a param object array to declare a method that accepts any number of arguments of objects, allowing the arguments to be passed of any type.
· you can even pass it no arguments at all,
· you can also call it by passing the null as the argument,
· you can pass it an actual array ,
· you can pass any other arguments of different types and these arguments will automatically be wrapped inside an object array.