C # required knowledge -- Variable Length Parameter -- Params,
When the number of parameters is variable, Params indicates that the number of parameters is unknown.
When using Params, you need to know the following:
1. If the parameters passed by the function contain multiple parameters, the parameter array marked with Params needs to be placed at the end
The figure is clearly displayed. You can only use the sort method of A without much explanation.
2. Params must be an array and must be a one-dimensional array.
3. Params cannot be combined with ref or out.
See the article http://www.cnblogs.com/hunts/archive/2007/01/13/619620.html of Hunts. C predecessors
4. The real parameter corresponding to the parameter array modified by Params can be an array name of the same type (Note: Only one array name is allowed, and multiple array names are not allowed ), it can also be any number of variables of the same type as the elements of the array.
DEMO code
Class Program {static void Main (string [] args) {// The display parameter is variable int I = Sum (1, 13, 23, 34); Console. writeLine (I); int j = Sum (1, 1, 3, 2, 4, 4, 44,555, 6); Console. writeLine (j); // The real parameter can be an array name int [] ArrayI = new int [5] {1, 2, 3, 4, 5 }; int ArraySum = Sum (ArrayI); Console. writeLine (ArraySum); Console. read ();} static int Sum (params int [] s) {int sum = 0; foreach (int I in s) {sum + = I ;}return sum ;}}