Optional parameter vs Empty parameters and overloaded constructor

To put it simply, let's 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 triggers the "Empty Parameter Function"? I understand method overloading, but why does the optional parameter function work as if the empty parameter function does not exist, would it work?

+3


source to share


1 answer


The next point regarding Overload Resolution , from MSDN , explains this solution:



If two candidates are considered equally good, preference is given to the candidate that has no optional parameters for which the arguments were omitted in the call. This is a consequence of the general preference for overload resolution for candidates with fewer parameters.

+7


source







All Articles