. A summary of considerations for methods in net

Source: Internet
Author: User
This paper summarizes in more 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.
Break can only terminate the current loop.

2. The method is to reuse a pair of available code.
A. For reusable code, select in VS, right-click refactoring extraction method. Can be automatically encapsulated into one method.
B. Programming, when we call an undefined method. Ctrl +. And then enter. The corresponding method is automatically generated.

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

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

5. Parameter modifier params denotes variable length parameter

Note: mutable parameters must be the last parameter!

Principle: At compile time, the compiler put our actual parameters into an array. And then pass it in. Personally think it is also a grammatical sugar. Ha ha

6. Parameter modifier ref

Represents a reference pass. For value types referenced by an assignment, if you want to invoke the method using a reference delivery method. Method parameters need to be decorated with ref.

Attention:
1) A value must be assigned before the parameter reference.
2) method, you can not assign a value to the ref parameter. (Note that it is possible not to, meaning that the assignment is also OK, not assigning a value.) )

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

You need to define this

public static void Swap (ref int A, ref int b) {      //implementation omitted .... }

When called

int a =1;   int b = 2;  Note that the value must be assigned in advance.  Swap (ref a, ref B);

7. Parameter modifiers out

Allows a function to output multiple values

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

2) Variables of out parameters do not need to be assigned before they are passed. (No sense)

8. Method overloading (overload) compile-time polymorphism

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

Attention:

Condition for method overloading

1) Same method name

2) different number of parameters

Or

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

Compile-time polymorphism, is static, although also known as the XX polymorphic, but with the object-oriented characteristics of the relationship does not have a penny.

Overloading is just a language feature, a syntax rule that is independent of polymorphism and is also object-oriented.

I hope this article is for everyone. NET programming has helped.

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.