Generally, the number of parameters is fixed, and the parameters defined as cluster types can achieve the purpose of variable number of parameters, but. NET provides a more flexible mechanism for implementing variable-number parameters, which is the use of the params modifier. The advantage of a variable number of parameters is that in some cases it is convenient to implement an indeterminate number of parameters, such as calculating the weighting of any number and linking any string to a string. Look at the example:
Copy Code code as follows:
public class Test2
{
public static void Main ()
{
ShowName ("Pawn");
ShowName ("Xiao Wang", "Xiao Liu");
}
public static void ShowName (params string[] names)
{
foreach (string name in Names)
{
Console.WriteLine (name);
}
}
}
Attention:
1, the params decorated parameters must be one-dimensional array.
2, params decorated parameter array, can be any type, as long as the array type is set to object.
3, params must be in the last one of the parameter list, and can only be used once.