Definition
The member signature contains the member name and parameter list. Each member signature must be unique in the type. The member names can be the same if the parameter list of the member is different. If two or more members of the type are similar members (methods, properties, constructors, etc.), they have the same name and different parameter lists, the same type of member is overloaded. For example, the array class contains two copyto methods. The first method uses an array and an int32 value. The second method uses an array and an int64 value.
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:
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:
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:
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:
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 you useOutAccording to the Conventions, such parameters should be the final parameters.
If you need scalability, use the longest overload as the virtual overload. A shorter overload must be called gradually.
The following code example demonstrates this.
Public void write (string message, filestream Stream)
{
This. Write (message, stream, false );
}
Public Virtual void write (string message, filestream stream, bool closestream)
{
// Do work here.
}
This method reduces unnecessary code and facilitates maintenance.
Do not use the ref or out modifier for overload members.
Public void write (string message, int count)
... Public void write (string message, out int count)