. NET Foundation 5

Source: Internet
Author: User
Tags mul

  In this article, we'll learn about the methods in C #--Functions

1. Function of the method

The function of the method: to reuse the code, when we write the same code repeatedly in a program, we can define the code that needs to be repeated in a method, only need to call it. This is also the encapsulation of the code.

Syntax for defining methods:

[access modifier] [StatiC] Return value type method name ([parameter list])

{method body;}

2. Places to be aware of

  1. The method can have one or more parameters, or no parameters. The parameters in the argument list correspond to local variables in the method.

2. If the method does not return a value, the return value type is void, and each method has only one return value (if you want to return multiple values, you can return an array or return by parameter) However, there are no return values allowed, except for the constructor and destructor methods.

3. In the method as long as the return, immediately exit the method, if try...cathy...finally, in the try inside if you encounter return,finally will still execute.

3. Method returns an example of multiple values

 a) return multiple values with an array

 static  int  [] Sum (int  NumA, int   NumB) { int  sum = NumA + NumB;     int  sub = NumA- NumB;  int  [] arrres = new  int  [2    sum;    arrres[  Sub;  return   Arrres;}  

b) create an output parameter with the Out keyword and return multiple values

Because the address of the parameter is passed when the out modifier parameter is used, the modifications we make to the parameters affect the actual argument.

if an out-of-decorated parameter is not assigned within a method, the compilation will error: " an out parameter must be assigned before leaving the current method " .

            intNumA =Ten, NumB =7; intsum, Sub; Sum= Test (NumA, NumB, outSUB);//to return values by parameterConsole.WriteLine ("sum = {0} sub = {1}", sum, sub); //sum = 3 Sub =                Static intTest (intAintB out intsub) {            //You cannot use a sub until it has been assigned a value//Sub + = 1; error, using unassigned out parameter "sub"//sub = 1; Correct, assign a value to subSub = A-b; intsum = a +b; returnsum; }                    

  c) the difference between out and ref:

Out has only outgoing functionality that must be assigned within the method, ref can be passed in and out, with emphasis on incoming, and must be assigned before calling the method.

C # Masks Pointers in C + + with the keyword ref

            intx =2, y =3; intMul =0; Fun (x, Y,refmul); Console.WriteLine ("Mul = {0}", Mul); //Mul = 6            Static voidFun (intXintYref intmul) {Mul= x *y; }            

4. variable parameters in the method

Multiple parameters can be allowed in Console.WriteLine ("A = {0}, B = {1}", 0, 1).

public static void WriteLine (string format, params object[] arg);

The two parameters in the above method accept the values: "a = {0}, B = {1}", an array of type Object arg={0,1}

  The function of the params is to combine multiple parameters into an array of fear; If the variable parameter is not passed, the variable parameter is an array object of length 0.

params -Modified parameters must be placed at the right end of the parameter list in the method

        Static voidMain (string[] args) {            int[] Arrage = {1,2,3,4,5,5, the }; Paramestest ("Andy Lau", arrage);//variable parameters can be passed directly into the array object .Paramestest ("Andy Lau",1,2,3);//variable parameters can also be passed as a single valueParamestest ("Andy Lau");//mutable arguments can be passed, so the variadic parameter parameters in the method are a Length=0 array objectConsole.ReadLine (); }        Static voidParamestest (stringNameparams int[] Ages)//The mutable parameter must be placed in the last of the method parameter list{Console.WriteLine ("hello? ~~{0}", name); Console.WriteLine ("Ages==null:"+ Ages = =NULL); Console.WriteLine ("ages. Length="+ages.            Length); foreach(intAgeinchages) {Console.WriteLine ("the current loop to the age="+Age );        } Console.WriteLine (); }

Output:

5. Overloading of methods

Typically in the same class, the method name is the same, and the method's argument list differs to form the overloaded method.

Note: The overloads of the method have no relation to the return value.

For example, the overloads of the Console.WriteLine

  

. NET Foundation 5

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.