Net framework 4.0 adds support for optional parameters. In fact, it is very easy to assign a default value to the parameter.
As follows:
[Csharp]
Static string GetStr (string s = "a", int I = 10, string r = "rrrr ")
{
Return s + I + r;
}
Static string GetStr (string s = "a", int I = 10, string r = "rrrr ")
{
Return s + I + r;
}
You can use the following methods to call
[Csharp]
GetStr ();
GetStr ("abcde ");
GetStr ("abcde", 100 );
GetStr ("abcde", 100, "hjklmn ");
GetStr ();
GetStr ("abcde ");
GetStr ("abcde", 100 );
GetStr ("abcde", 100, "hjklmn ");
In addition, the order of parameters cannot be changed. You cannot skip a parameter and use the following parameter type. Therefore, there will be a problem, what if I want to call this method to only use the first and third parameters.
In fact, in the call of optional parameters, Microsoft launched the named parameters, that is
[Csharp]
GetStr (s: "abcde", r: "hijklmn ");
GetStr (s: "abcde", r: "hijklmn"); you only need to mark the parameter name. (The name parameter can only be the parameter name of the original method followed by the colon ":")
In this way, future overloading can save a lot of trouble. Removes the need for code overloading.