1. Array of parameters. C # features that allow a parameter array to be specified at the end of a function parameter, which can be called with an indefinite number of arguments, defined with the params keyword
static double sumvals (params double [] vals) { double sum = 0 ; foreach (double i in Vals) {sum += I; return sum;} // call Sumvals (1 , 2 , 3 );
2. Value parameters and reference parameters. Reference parameters are specified using the keyword ref . Both function definitions and function calls use ref
Static void Showdouble (refint val) { 2; Console.WriteLine ("Val doubled is {0}", Val);}
Called, SRC must be non-const,src to initialize the assignment.
int src = 1;
Showdouble (ref SRC);
3. Output parameters. Using the Out keyword, like the reference parameter, is different from ref, the out parameter can be an unassigned variable, and the suggestion is that the value will be modified instead of meaningless.
Static DoubleMaxval (Double[] Vals, out intMaxindex) { DoubleMaxval = vals[0]; Maxindex=0; for(inti =1; I < Vals. Length; i++) { if(Maxval <Vals[i]) {Maxval=Vals[i]; Maxindex=i; } } returnmaxval;}//UseDouble[] dvals={1,2,3,4,5,Ten, the, -};intDindex; Console.WriteLine ("{0},{1}", Maxval (Dvals, outDindex), Dindex);
4. Overloading of functions. the signature of a function includes the name of the function and its arguments, including the type of the parameter and the keyword (out/ref), excluding the return type of the function.
5. Delegation. A type that stores references as functions. The life of a delegate is similar to a function, without a function body, using the keyword delegate. The declaration of a delegate specifies a return type and a list of parameters that are mostly used for event handling.
Delegate DoubleProcessdelegate (DoubleV1,Doublev2);Static DoubleMultiply (DoubleV1,Doublev2) { returnV1 *v2;}Static DoubleDivide (DoubleV1,Doublev2) { returnV1/v2;}//Useprocessdelegate process;process=Newprocessdelegate (Divide);//The first use of the process=multiply;//The second use of process (1,2);
C # supplements (second, function)