I accidentally overloaded a constructor in C# as follows:
public MyClass(string myString)
{
// Some code goes here
}
public MyClass(string myString, bool myParameter = false)
{
// Some different code here
}
With this code my project compiled fine. If I call the constructor with just a string
argument, how does C# decide which constructor I want to use? Why is this functionality syntactically allowed?
Why is this functionality syntactically allowed?
In terms of the IL generated, the second constructor is still two arguments. The only difference is that the second argument has an attribute providing the default value.
As far as the compiler is concerned, the first is still technically a better fit when you call a constructor with a single string. When you call this with a single argument, the best match is the first constructor, and the second will not get called.
The C# specification spells this out. In 7.5, it states "... instance constructors employ overload resolution to determine which of a candidate set of function members to invoke." The specific rules are then specified in 7.5.3.2, where this specific rule applies:
Otherwise if all parameters of MP have a corresponding argument whereas default arguments need to be substituted for at least one optional parameter in MQ then MP is better than MQ.
In this case, MP (your first constructor) has all arguments, but MQ (your second) needs "at least one optional parameter."
From MSDN: (emphasis added)
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.
Use of named and optional arguments affects overload resolution in the
following ways:
A method, indexer, or constructor is a candidate for execution if each
of its parameters either is optional or corresponds, by name or by
position, to a single argument in the calling statement, and that
argument can be converted to the type of the parameter.
If more than one candidate is found, overload resolution rules for
preferred conversions are applied to the arguments that are explicitly
specified. Omitted arguments for optional parameters are ignored.
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.
Quoted from MSDN.
Method with optional parameter will be hidden by overload.
There is even a ReSharper rule for that which warns you.
And here you can find useful discussion on why it's allowed.
Some people even think that it's not an issue at all.
http://youtrack.jetbrains.com/issue/RSRP-287157
I bet it will select the first one as more appropriate.
Use of named and optional arguments affects overload resolution in the following ways:
A method, indexer, or constructor is a candidate for execution if
each of its parameters either is optional or corresponds, by name or
by position, to a single argument in the calling statement, and that
argument can be converted to the type of the parameter.
If more than one candidate is found, overload resolution rules for
preferred conversions are applied to the arguments that are
explicitly specified. Omitted arguments for optional parameters are
ignored.
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.