To determine whether a method constitutes an overload, the following conditions are met:
◆ In the same class;
◆ Method names are the same;
◆ The parameter list is different.
Precautions should be taken when designing the overload method
1. Avoid modifying the parameter name in the overload. If a parameter of one overload is the same as that of another overload, the two parameters should have the same name.
For example, do not perform the following operations:
Copy codeThe Code is as follows:
Public void Write (string message, FileStream stream ){}
Public void Write (string line, FileStream file, bool closeStream ){}
The correct definitions of these reloads are as follows:
Copy codeThe Code is as follows:
Public void Write (string message, FileStream stream ){}
Public void Write (string message, FileStream stream, bool closeStream ){}
Maintain the order consistency of the overloaded member parameters. In all overload operations, parameters with the same name must be in the same position.
For example, do not perform the following operations:
Copy codeThe Code is as follows:
Public void Write (string message, FileStream stream ){}
Public void Write (FileStream stream, string message, bool closeStream ){}
The correct definitions of these reloads are as follows:
Copy codeThe Code is as follows:
Public void Write (string message, FileStream stream ){}
Public void Write (string message, FileStream stream, bool closeStream ){}
The above two statements have a clear structure and enhance the readability of the Code, making them more suitable for standardization.
This criterion has two constraints:
If the variable parameter list is used for overloading, the list must be the last parameter.
If the out parameter is used for heavy load, such parameters should be used as the final parameter according to the Conventions.
If you need scalability, use the longest overload as the virtual overload. A shorter overload must be called gradually.
Differences from override Rewriting
Override refers to the inheritance relationship between the parent class and the Child class. These methods share the same name and parameter type.