Currently I'm using .Net 3.5 so please let me know if it has been fixed in a later version.
Currently I have 2 methods with the following signatures:
void Method1(string, string)
void Method1(string, params string[])
If I make a call such as this
Method1("test1", "test2")
how does the compiler know which method to call? Why does .Net allow this?
I assume that in the IL, the resulting code is different and therefore allowed, but it shouldn't be, because you can get unexpected results. Is there a good reason why this is allowed?
Thanks in advance.
how does the compiler know which method to call?
It follows the overload resolution rules listed in the C# language specification. In particular, in section 7.5.3.2 (looking at the C# 4 spec, but I believe C# 5 has the same numbering here) - "Better Function Member":
In case the parameter type seqquences are equivalent [...] the following tie-breaking ruls are applied, in order, to determine the better function member:
- ...
- Otherwise, if MP is applicable in its normal form and MQ has a
params
array and is applicable only in its expanded form, then MP is better than MQ.
So in your example, it's going to call the first overload.
Why does .Net allow this?
Because it can be useful in various cases (e.g. Console.WriteLine
for a start).
I assume that in the IL, the resulting code is different and therefore allowed, but it shouldn't be, because you can get unexpected results.
You only get unexpected results if you don't expect the C# compiler to follow its specification. In that case, pretty much any behaviour can be unexpected.
See more on this question at Stackoverflow