Possible Duplicate:
Who actually last decide what is the Generic Type?
As my question title says can anyone please explain me what is bounds (upper bound, lower bound, exact bound) and how do they play role in type inference with examples?
Consider simple code :
void func<T> ( T firstparam , T secondparam) { }
and caller call it
func( 23 , 23.23 );
What are the bounds while type inference process happens and how they are used by inference process?. Should my presented example is way trivial and will not have any upper bounds etc, please include your own example which will represent the idea.
In your examples there are no bounds in type inference. If you call it like
Then candidates for types would be
intanddouble.Intcan be casted implicitly todoublebut not vise-versa, so the fixed type forTisdouble. This has nothing to do with bounds, but type compatibility.But however, if you call it like
Then the upper bound for
Twill beobject, the lower bound will bedouble. In such case fixed type forTwill beobject.Eric Lippert describe what bounds are and why they separate lower, upper and exact bounds at blogpost on How do we ensure that method type inference terminates?
Jon Skeet describe in detail the process of type inference at 9.4.3 Two-phase type inference in his wonderful book C# in Depth. Please read carefully his description for type inference for listing 9.11