I have this function
public static T2 MyFunc( T1 a, T1 b, T2 c)
{
return c;
}
I\'m creatin
"Who actually decide about the T1 type ? (p ? p2 ? )"
Normally, the C# compiler decides this. If one of the method arguments is dynamic, then the decision is done at runtime (by the Microsoft.CSharp library).
In both cases, the type inference algorithm described in the C# specification is applied:
Both the types of p and p2 are added to T1's set of lower bounds (upper bounds are also possible, but only when contravariant generics are involved).
Then, the compiler picks one of the types in the set of bounds that also satisfies all other bounds.
When there is only one bound because p and p2 have the same type, this choice is trivial.
Otherwise (assuming only lower bounds are involved), that means the compiler picks a type so that all other candidate types are implicitly convertible to that type (what svick's answer describes).
If there is no unique such choice, type inference fails - another overload gets chosen if possible, otherwise a compiler error occurs (when the decision is done at runtime (dynamic), an exception is thrown instead).