C USES the params modifier to implement the variable length parameter passing method

  • 2020-05-17 06:17:12
  • OfStack

The advantage of a variable number of parameters is that it can be easily implemented in some cases where the number of parameters is uncertain, such as calculating the weighted sum of any number, linking any string to 1 string, and so on. Here's an example:


public class Test2 
{ 
    public static void Main() 
    { 
        ShowName(" small A"); // You can specify parameters of any length or pass different types, but change the parameter type to object
        ShowName(" small A", " small B"); 
    } 
    public static void ShowName(params string[] names) 
    { 
        foreach (string name in names) 
        { 
        Console.WriteLine(name); 
        } 
    } 
}


Related articles: