C # supplements (second, function)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.