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 );
}
}
}