In your call, you do not specify a type argument - therefore, the compiler would have to specify a type T It cannot do this for your second method, because a type parameter is never mentioned in declared parameters. Therefore, this overload is not applicable and is ignored.
If you specify a type argument to call, for example. any of
DoSomething<int>(1) DoSomething<object>(1) DoSomething<string>(1)
... then in all cases a second overload will be called.
From section 7.6.5.1 of the C # 5 specification (method calls) when constructing a set of candidate methods:
- If F is generic and M does not have a list of type arguments, F is a candidate when:
- Type inference (Β§7.5.2) succeeds by listing the type arguments to invoke and
- When the inferred type arguments are replaced by the corresponding method type parameters, all constructed types in the parameter list F satisfy their restrictions (Β§4.4.4), and the parameter list F is applicable with respect to A (Β§ 7.5.3.1).
As type inference does not work, the second method is not in the set of candidates, so by the time we get to the real resolution of overloading, the set has only one method (the first).
source share