To put it simply, say you have two methods:
public static void WriteMessage()
{
Console.Write("Empty Parameter Function");
}
public static void WriteMessage(string data = "Some Data")
{
Console.Write("Optional Parameter Function");
}
Why is it that if you call the WriteMessage function without any parameters, it runs the "Empty Parameter Function"? I understand method overloading, but why does not the optional parameter function run as if the empty parameter function didn't exist, it would run?
The following point regarding Overload Resolution , from MSDN , explains that decision:
If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.