C # Learning 4

Source: Internet
Author: User

Function Overloading is the same as the Chinese method name of the same class. The parameter list is different. The parameter list is different, including the number of parameters in the method or the parameter data type is different.

First, it is a common overload. You can modify the number and Data Type of parameters.


[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace chongzai
{
Class Program
{
Static void Main (string [] args)
{
Program p = new Program ();
P. run ();
}
Void run ()
{
Int arg1 = 10;
Float arg2 = 12.5f;
Int arg3 = 30;
Dowork (arg1, arg2 );
Dowork (arg1, arg2, arg3 );
Dowork (arg2, arg3 );
}
Void dowork (int intdata, float floatdata, int moreintdata)
{
Console. WriteLine ("intdata: {0}, floatdata: {1}, moredata: {2}", intdata, floatdata, moreintdata );
}
Void dowork (int intdata, float floatdata)
{
Console. WriteLine ("intdata: {0}, floatdata: {1}", intdata, floatdata );
}
Void dowork (float floatdata, int moreintdata)
{
Console. WriteLine ("floatdata: {0}, moreintdata: {1}", floatdata, moreintdata );
}
}
}

You can also assign default values to parameters for reload. Note that parameters without default values are written before, and parameters with default values are written after.

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace chongzai
{
Class Program
{
Static void Main (string [] args)
{
Program p = new Program ();
P. run ();
}
Void run ()
{
Int arg1 = 10;
Float arg2 = 12.5f;
Int arg3 = 30;
Dowork (arg1 );
Dowork (arg1, arg2 );
Dowork (arg1, arg2, arg3 );
Dowork ();
}
Void dowork (int intdata = 0, float floatdata = 0.0f, int moreintdata = 0)
{
Console. WriteLine ("intdata: {0}, floatdata: {1}, moredata: {2}", intdata, floatdata, moreintdata );
}
}
}

You can also specify the parameter value to reload.

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace chongzai
{
Class Program
{
Static void Main (string [] args)
{
Program p = new Program ();
P. run ();
}
Void run ()
{
Int arg1 = 10;
Float arg2 = 12.5f;
Int arg3 = 30;
Dowork (arg1 );
Dowork (moreintdata: arg3 );
Dowork (arg1, arg2 );
Dowork (floatdata: arg2, moreintdata: arg3 );
Dowork (arg1, arg2, arg3 );
Dowork ();
}
Void dowork (int intdata = 0, float floatdata = 0.0f, int moreintdata = 0)
{
Console. WriteLine ("intdata: {0}, floatdata: {1}, moredata: {2}", intdata, floatdata, moreintdata );
}
}
}

 

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.