C # Heavy Load

Source: Internet
Author: User

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)

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.