Reputation: 5644
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?
Upvotes: 12
Views: 19702
Reputation: 7931
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
Upvotes: 1
Reputation: 564423
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."
Upvotes: 11
Reputation: 2687
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.
Upvotes: 0
Reputation: 2281
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.
Upvotes: 13
Reputation: 19027
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.
Upvotes: 2