Summary of C # method Overloading

Source: Internet
Author: User

1. What is method overload first?

Method overloading refers to a technique in which methods in the same class have the same name and different parameters. When calling a method, you can select a method that matches the method to perform the operation according to the form of real parameters.

Different parameters here refer to the following situations:

① Different parameter types

② The number of parameters varies

③ The numbers of parameters are the same, and their order is different.

Note: The system will consider the two cases of the same method. Such two methods cannot be in the same class; otherwise, the system will report an error.

(1) Two methods with different return types, with the same method name and parameter count, order, and type

(2) Two methods with the same return type, the same number, order, and type of method names and parameters, but different parameter names

For example:

Copy codeThe Code is as follows: ① protected void (){

Console. WriteLine ("aaaaaaaaaaaa ");}

② Protected void A (string s, int a) {// correct method overload

Console. WriteLine ("ccccccccccc ");}

③ Protected void A (string a, int s ){

Console. WriteLine ("ccccccccccc ");

}

④ Protected void A (int a, string s ){

Console. WriteLine ("bbbbbbbbbbbb ");

}

① And ② are method Overloading

② Compare with ③ and ④: ② and ③ are the same method, because they only have different parameter names. ② and ④ are correct method overloading because their Parameter order is different.

2. Applicability: common methods and constructor methods are supported.

3. The following conditions determine whether a method constitutes a heavy load:

◆ In the same class;

◆ Method names are the same;

◆ The parameter list is different.

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.