Is there any way in C# to achieve the following:
class MyClass<T> where T : BaseTypeInner {}
class BaseTypeInner {}
class A : BaseTypeInner {}
class B : BaseTypeInner {}
void Main()
{
MyClass<BaseTypeInner> variant;
variant = new MyClass<A> (); // ERROR: Cannot implicitly convert type 'UserQuery.MyClass<UserQuery.A>' to 'UserQuery.MyClass<UserQuery.BaseTypeInner>'
variant = new MyClass<B> ();
}
In C# only interfaces can be variant. Quoting C# spec:
So you could declare a generic, covariant interface
IBaseClass<out T>
, makeBaseClass<T>
implement it, and later on cast toIBaseClass<BaseTypeInner>
instead of casting to the class.No, however it is possible to provide custom
CastUp
/CastDown
methods to achieve this.ImmutableArray<T>
, part of the .NET BCL, does this.A possible solution is to have a base type for the base generic type:
But, that's not a good idea because I'm using a lower class that's not generic so I'm losing all the details and restrictions I get from using type parameters.