Reputation: 322
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?
Upvotes: 5
Views: 365
Reputation: 10356
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.
Upvotes: 8