Overloading of C # methods

Source: Internet
Author: User

1. First explain what is method overloading?


Method overloading refers to a technique in which a method with the same name in the same class, with different parameters, chooses to perform an operation on the method that matches the argument, in the form of an argument.


The different parameters mentioned here refer to the following situations:

① parameter types are different

The number of ② parameters is different

The number of ③ parameters is the same as their order of precedence.


Note: The system will consider two cases of the same method, so that the two methods can not be in the same class, or the system will error.

⑴ return types, two methods with the same method name and number of arguments, order, and type

⑵ return type is the same, the method name and parameter number, order, type are the same two methods, but the name of the parameter is different


For example:


①protected void A ()

{

Console.WriteLine ("Aaaaaaaaaaaa");

}


②protected void A (string s, int a)

{

Console.WriteLine ("CCCCCCCCCCC");

}


③protected void A (string a, int s)

{

Console.WriteLine ("CCCCCCCCCCC");

}


④protected void A (int a,string s)

{

Console.WriteLine ("BBBBBBBBBB");

}



①, ②, ④ are method overloads, and ①, ③, and ④ are method overloads;

② and ③ are the same method because they are just different names of parameters.


2. Scope of application: Common method and construction method can be


3. Determining whether a method constitutes an overload has the following conditions:

In the same class;

Method name is the same;

The parameter list is different.


Overloading of C # methods

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.