Pass a variable number of parameters to the method.
Public sealed class program {public static void Main () {DisplayTypes (new object (), new double [] {}, "aaa", 5 );} private static void DisplayTypes (params Object [] objects) {if (objects! = Null) {foreach (Object o in objects) Console. WriteLine (o. GetType () ;}console. ReadKey ();}}
Output result // System. Object // System. Double [] // System. String // System. Int32
About params
C # In development language, params is a keyword. You can specify the parameter method parameter when the number of parameters is variable. It is useful when the number of function parameters is variable and the difference in code executed is very small! After the params keyword in the method declaration, no other parameters are allowed, and only one params keyword is allowed in the method declaration. You need to know the following about parameter Arrays. (1) If a parameter array is included in the parameter table, the parameter array must be at the end of the parameter list; (2) the parameter array must be a one-dimensional array; (3) it is not allowed to combine the params Modifier with the ref and out modifiers. (4) The real parameters corresponding to the parameter array can be array names of the same type, it can also be any number of variables of the same type as the elements of the array. (5) if the real parameter is an array, it is passed by reference. If the real parameter is a variable or expression, it is passed by value. (6) usage: variable method parameters, also called array parameters, are suitable for passing a large number of array set parameters when the number of method parameters is unknown, you can use the params keyword to specify multiple method parameters in the form: the method modifier returns the type method name (params type [] variable name), which is an extension of a one-dimensional array.