. NET methods of attention Summary _ practical skills

Source: Internet
Author: User
Tags modifier terminates

This article is summarized in detail. NET in the method. Share to everyone for your reference. The specific analysis is as follows:

1. Return in the method terminates the entire method segment.
The break terminates only the current loop.

2. The method is the reuse of a pair of usable code.
A. For reusable code, select in VS, right-click to refactor the extraction method. Can be encapsulated automatically into one method.
B. In programming, when we call an undefined method. Ctrl +. and enter. The corresponding method is generated automatically.

3. For the return value of the method, if the return value is defined, the method must have a corresponding returns.
No return worthwhile method can be used with void

4. Constructors do not need to be decorated with keywords such as void or int.

5. Parameter modifier params representing variable length parameters

Note: The variable parameter must be the last argument!

Principle: The compiler at compile time, we have to get the actual parameters of an array. Then pass in. Personally feel it is also a grammatical candy. Ha ha

6. Parameter modifier ref

Represents a reference pass. For the value type of an assignment reference, if you want to invoke the method with a reference-passing method. Method parameters need to be decorated with ref.

1 must be assigned before the parameter reference.
2) method, you can not assign a value to a ref parameter. (Note that you can not, meaning that the assignment is also a row, do not assign value also line.) )

For example, the method requires the value of two variables to be exchanged.

You need to define this

Copy Code code as follows:
public static void Swap (ref int A, ref int B)
To achieve the omission of ....

When called

Copy Code code as follows:
int a = 1; //
int b = 2; Note that the value must be assigned in advance.
Swap (ref a, ref B);

7. Parameter modifiers out

Allow functions to output multiple values

1 The method must assign a value to the out parameter.

2 the variable of out parameter does not need to be assigned before passing. (No sense)

8. Method overload (overload) compile-time polymorphism

Method names are the same, and methods with different parameters are called method overloads.


Condition of method overload

1) method name is the same

2) The number of parameters is different


3 The number of parameters is the same and the parameter types are different.

Compile-time polymorphism, is static, although also known as XX polymorphism, but with object-oriented characteristics do not have a dime relationship.

Overloading is just a linguistic feature, a grammatical rule that is independent of polymorphism and not object oriented.

You want this article to be addressed to everyone. NET program design helps.

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.