Which of the following conditions and features are involved in function overloading in C #?

Source: Internet
Author: User
In C #, what are the conditions and features for function overloading?
1. function name;

2. function return value type;

3. function parameter type;

4. Number of function parameters;

5. function parameter order;

My personal summary is as follows:

◆ Function names must be the same to form function overloading;

◆ Function return value type: it can be the same or different (Note: The return type of the function is insufficient to distinguish between the two overloaded functions );

◆ Function parameter type: It must be different;

◆ Number of function parameters: they can be the same or different

◆ Function parameter order: the order can be the same or different;

◆ Note: The parameter table comparison process has nothing to do with the parameter name.

The preceding section describes that the signature of a function must be matched when a function is called. This indicates that multiple functions need to operate on different types of variables. Function overload allows you to create multiple functions with the same name. These functions can use different parameter types.

For example, the following code is used, which contains a maxvalue () function ():

Class Program

{

Static int maxvalue (INT [] intarray)

{

Int maxval = intarray [0];

For (INT I = 1; I <intarray. length; I ++)

{

If (intarray [I]> maxval)

Maxval = intarray [I];

}

Return maxval;

}


Static void main (string [] ARGs)

{

Int [] myarray = {1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };

Int maxval = maxvalue (myarray );

Console. writeline ("the maximum value in myarray is {0}", maxval );

Console. readkey ();

}

}

This function can only be used to process int arrays. To provide functions with different names for different parameter types, you can rename the preceding functions as intarraymaxvalue () and add the doublearraymaxvalue () function to process other types. In addition, you can add the following functions in the Code:

...

Static double maxvalue (double [] doublearray)

{

Double maxval = doublearray [0];

For (INT I = 1; I <doublearray. length; I ++)

{

If (doublearray [I]> maxval)

Maxval = doublearray [I];

}

Return maxval;

}

...

The difference here is that the double value is used. The function name maxvalue () is the same, but its signature is different. Defining two functions with the same name and signature is incorrect, but it is feasible because these two functions have different signatures.

Now there are two versions of maxvalue (), whose parameters are int and double arrays, respectively, return the maximum values of an int or double.

The advantage of this Code is that you do not need to specify which function to use. You only need to provide an array parameter to execute the corresponding function based on the parameter type used.

Pay attention to another feature of intelliisense in. If there are two functions in the application, and you need to enter the function name in main (), VS can display the available overload functions. If you enter the following code:

Double result = maxvalue (

Vs provides the maxvalue () version information. You can use the up and down arrow keys to scroll between them, as shown in 6-9.





Figure 6-9

When reloading a function, it should include all aspects of the function signature. For example, there are two different functions with value parameters and reference parameters:

Static void showdouble (ref int Val)

{

...

}


Static void showdouble (INT Val)

{

...

}

The version used is determined based solely on whether the function call contains the ref keyword. The following code calls the reference version:

Showdouble (ref Val );

The following code calls the value version:

Showdouble (VAL );

In addition, functions can be distinguished by the number of parameters.

C # constructor overload usage
Public class languageconfiguration
{
Public languageconfiguration ()
{
Initclass ();
}
Private void initclass ()
{
Initclass ("languageconfigfile ");
}
Public languageconfiguration (string languagexml)
{
Initclass (languagexml );
}

Private void initclass (string extends agexml)
{

}
}

Usage
Languageconfiguration lc = new languageconfiguration ("operationlanguageconfigfile ");

 

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.