Last question
Since you specified (by parameter name) that it should call the overload that takes an int
parameter, the compiler has no idea what to pass for T
.
First question
Because of this, Foo(5)
only matches one overload (Foo<T>()
).
Therefore, it must only call Foo<T>()
.
Second question
When you explicitly specify a type argument (<int>
), both overloads are applicable.
In that case, Foo(int)
is better, since its parameter is not of generic type.
As per the C# spec §7.5.3.2:
- Otherwise, if MP has more specific parameter types than MQ, then MP is better than MQ. Let {R1, R2, …, RN} and {S1, S2, …, SN} represent the uninstantiated and unexpanded parameter types of MP and MQ. MP’s parameter types are more specific than MQ’s if, for each parameter, RX is not less specific than SX, and, for at least one parameter, RX is more specific than SX:
- A type parameter is less specific than a non-type parameter.
(emphasis added)