Asp. net method how to overload the necessary conditions _ practical skills

Source: Internet
Author: User
there are several conditions for determining whether a method constitutes an overload:

In the same class;

The method name is the same;

The argument list is different.

you should be aware of the issues when designing overloaded methods

1 avoid arbitrarily changing the name of the parameter in the overload. If one of the overloads of an overload represents the same input as a parameter of another overload, the two arguments should have the same name.

For example, do not perform the following actions:
Copy Code code as follows:

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
Copy Code code as follows:

public void Write (string message, FileStream stream) {}
public void Write (String message, FileStream Stream,bool closestream) {}

Maintains sequential consistency of overloaded member parameters. In all overloads, the position of the same parameter should be the same.

For example, do not perform the following actions:
Copy Code code as follows:

public void Write (string message, FileStream stream) {}
public void Write (FileStream stream, String message, bool Closestream) {}

The correct definitions of these overloads are as follows:
Copy Code code as follows:

public void Write (string message, FileStream stream) {}
public void Write (String message, FileStream Stream,bool closestream) {}

The above two kinds of writing structure is clear, enhances the readability of the code, more suits the specification.

This guideline has two constraints:

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

If the overload takes an out parameter, by convention, such parameters should be the last argument

If extensibility is required, the longest overload is used as a virtual overload. A shorter overload should only step through a longer overload.

the difference with override rewrite

Override refers to the inheritance of methods between a parent class and a subclass that have the same name and parameter type.
Related Article

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.