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.
Inference doesn't consider the return type; you can, however, try splitting the generics; for example, you could write code to allow:
.Cast().To<Type2>()
by having (untested; indicative only)
public static CastHelper<T> Cast<T>(this T obj) {
return new CastHelper<T>(obj);
}
public struct CastHelper<TFrom> {
private readonly TFrom obj;
public CastHelper(TFrom obj) { this.obj = obj;}
public TTo To<TTo>() {
// your code here
}
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With