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.