Summary of precautions for methods in. NET and. net

Source: Internet
Author: User

Summary of precautions for methods in. NET and. net

This article summarizes in detail the precautions for the. NET method. Share it with you for your reference. The specific analysis is as follows:

1. return in the method terminates the entire method segment.
Break can only terminate the current loop.

2. The method is to reuse a pair of available code.
A. for reusable code, right-click vs to reconstruct the extraction method. It can be automatically encapsulated into a method.
B. in programming, when we call an undefined method. Ctrl +. Then Enter. The corresponding method is automatically generated.

3. If the return value is defined for the method return value, the corresponding return must exist in the method.
Void can be used if no value is returned

4. constructors do not need to be modified with keywords such as void or int.

5. The parameter modifier params indicates a variable length parameter.

Note: variable parameters must be the last parameter!

Principle: during compilation, the compiler converts our actual parameters into an array. Then pass in. I personally think it is also syntactic sugar. Haha

6. Parameter modifier ref

Indicates that the reference is passed. For the value type referenced by the value assignment, if you want to use the reference transfer method to call the method. The method parameters must be modified with ref.

Note:
1) values must be assigned before parameter reference.
2) In the method, you can leave the ref parameter value unspecified. (Note that the value can be left blank, meaning that the value can also be a row without a value .)

For example, the method requires that the values of two variables be exchanged.

You need to define
Copy codeThe Code is as follows: public static void Swap (ref int a, ref int B)
{
// Implementation is omitted .......
}

Call time
Copy codeThe Code is as follows: int a = 1 ;//
Int B = 2; // note that values must be assigned in advance.
Swap (ref a, ref B );

7. Parameter modifier out

Allow the function to output multiple values

Note:
1) The out parameter must be assigned a value in the method.

2) variables of the out parameter do not need to be assigned a value before being passed. (Meaningless)

8. Method overload polymorphism during compilation

Methods With the same name and different parameters are called method overloading.

Note:

Conditions for method Overloading

1) Same method name

2) the number of parameters varies.

Or

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

During compilation, polymorphism is static. Although it is also called XX polymorphism, it has nothing to do with object-oriented features.

Overload is just a language feature. It is a syntax rule and has nothing to do with Polymorphism or object-oriented.

I hope this article will help you with the. NET program design.

Related Article

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.