C # Grammar Review 2

Source: Internet
Author: User

Fifth Chapter Method

1, the method is a code with a name

Include: Method body, Method header

Local variables must be assigned to perform the following operations. Instance variables are implicitly initialized. Sometimes, type inference can be used with the Var keyword, similar to auto in C + +. Used for local variables.

In C #, you cannot declare another local variable with the same name within the valid range of the first name.

2, local constants: Local constants. Const keyword. Const double PI = 3.1415926; Determines its value at compile time.

3. Parameters: Form participates in the actual parameter. There are several parameters as well: value parameter/reference parameter.

Value types and value parameters are two different concepts: A value type is the type itself that contains its value. The value parameter is to copy the value of the argument to the formal parameter.

Void MyMethod (MyClass F1, int F2)

{

F1.val + = 5;

f2+= 5;

}

Call: MyMethod (A1,A2);

Execution process:

When the method starts, the system allocates space for the parameter in the stack and copies the value from the actual parameter. A1 is a reference type, so the reference is copied but points to the same.

When using reference parameters, you must add the keyword ref. void method (ref int val) {xxxx}, methods call Way (ref y);//variables must be used.

Example:

Void MyMethod (ref MyClass f1,ref int F2)

{

F1.val + = 5;

f2+= 5;

}

Call: MyMethod (ref a1,ref A2);

Execution process: does not open up new memory units, just set the parameter name as the alias of the argument. A1 and F1, refer to the same location.

Reference types as value parameters and reference parameters:

When we modify a member of a reference parameter, the effect is the same whether it is a value argument or a reference parameter. However, when we go to modify the reference type itself, it is different from the reference parameter as a value parameter.

Take a look at the following graphic:

4. Output parameters

Need to add keyword out

Void Mythod (out int val); The formal parameter is also an alias of the argument, and we will know the value of the argument you passed in after the method body. An argument is assigned a value inside the method body.

5. Parameter array

The previous argument corresponds to a formal parameter, and now multiple arguments correspond to a particular parameter.

Keywords: params such as void Mythod (params int[] vals) {}

Call: int[] arrays = {1, 2, 3}; Mythod (arrays);

When invoked, if it is passed with a value type, that is, the argument is a value, the array parameter is a value type, and the argument is not affected.

If a reference type is passed, that is, the argument is an array name, the array parameter is a reference type, and the argument is affected by the method.

6. Method overloading

Method name is the same, method features can be different.

7. Named parameters

Give the parameter a name so that we can call it in any order.

Void Mythod (int A, int b, int c);

The name and value of the parameter: Specify the name and value when calling. Mythod (C:2, A:4, b:1);

8. Optional Parameters

That is, we can call this parameter when we call it, or we can omit this parameter. At this point, the parameter must be given a default value at the time of declaration.

Void Mythod (int A, int b, int c = 2);

First required parameter + optional parameter + params parameter.

9, Stack frame: is a method into the stack.

C # Grammar Review 2

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.