Why must I provide explicitly generic parameter types While the compiler should infer the type?
public static T2 Cast<T1,T2>(this T1 arg) where T2 : class where T1 : class
{
return arg as T2;
}
Sample Usage:
objOfTypeT2 = objOfTypeT1.Cast<TypeT1,TypeT2>();
Compared to my desired usage with a more intelligent compiler:
objOfTypeT2 = objOfTypeT1.Cast<TypeT2>();
or maybe I should be more intelligent :-)
Beware that I provide the return type. I want to not provide the object that I called the function on it, the method is an Extension Method.
The specification limits type parameter inference for generic methods to all or nothing. You can't have partial inference.
The rationale is probably simplifying type inference rules (that are already pretty complex, as they have to take into account overloading rules too).
I've used Marc Gravell's solution and like it, but I can present another alternative.
Because the generic parameters are inferred from the parameters, another option is to use an out parameter for the result instead of a return value.
This have been possible for a long time, but today's C# allows you to declare the variable inline, which I find to be a usable flow.
You can call this as follows
Inference doesn't consider the return type; you can, however, try splitting the generics; for example, you could write code to allow:
by having (untested; indicative only)