What are the prerequisites for how the ASP. NET method is overloaded

  • 2020-09-16 07:25:36
  • OfStack

There are several criteria for determining whether a method constitutes an overload:

◆ In the same class;

◆ The method name is the same;

◆ Parameter list is different.

There are 1 things you should pay attention to when designing overloaded methods

Avoid arbitrary changes to parameter names in overloads. If 1 parameter of an overload represents the same input as 1 parameter of another overload, the two parameters should have the same name.

For example, do not do the following:
 
public void Write(string message, FileStream stream){} 
public void Write(string line, FileStream file,bool closeStream){} 

The correct definitions of these overloads are shown below
 
public void Write(string message, FileStream stream){} 
public void Write(string message, FileStream stream,bool closeStream){} 

Preserves the order 1 cause of overloaded member parameters. In all overloads, the location of the parameter with the same name should be the same.

For example, do not do the following:
 
public void Write(string message, FileStream stream){} 
public void Write(FileStream stream, string message, bool closeStream){} 

These overloads are properly defined as follows:
 
public void Write(string message, FileStream stream){} 
public void Write(string message, FileStream stream,bool closeStream){} 

The above two ways of writing structure, enhance the readability of the code, more suitable for specification.

This criterion has two constraints:

If an overload takes a variable argument list, the list must be the last argument.

If an overload takes the out parameter, by convention it should be the last parameter

If extensibility is required, consider the longest overload as virtual overload. Shorter overloads should only call longer overloads incrementally.

The difference with override rewrite

Override refers to the inheritance relationship between superclasses and subclasses of methods that have the same name and parameter types.

Related articles: