In general, the number of parameters is fixed. The parameter defined as a cluster type can achieve a variable number of parameters,. NET provides a more flexible mechanism to implement variable number parameters, which is to use the params modifier. The benefit of a variable number parameter is that, in some cases, it is easy to implement an uncertain number of parameters. For example, you can calculate the weighted sum of any number and link any string to a string. Let's take a look at the example:
Copy codeThe Code is as follows:
Public class Test2
{
Public static void Main ()
{
ShowName ("Xiao Bing ");
ShowName ("John", "John ");
}
Public static void ShowName (params string [] names)
{
Foreach (string name in names)
{
Console. WriteLine (name );
}
}
}
Note:
1. The parameters modified by params must be a one-dimensional array.
2. The parameter array modified by params can be of any type, as long as the array type is set to object.
3. params must be at the end of the parameter list and can only be used once.