Is it possible to “upcast” from a generic class based on T, into a generic class based on something more general than T?
For instance, say I have a class named Derived that inherits from a class named Base. Can I ever do something like this:
List<Derived> der = new List<Derived>();
List<Base> bas = (List<Base>) der;
Or, using interfaces, is it ever possible to do something like this:
List<MyClonableType> specific = new List<MyClonableType>();
List<IClonable> general = (List<IClonable>)specific;
As written here, each of these examples fails with an InvalidCastException. The question we’re arguing about is whether it’s actually impossible, or simply a syntax error that could be fixed if we knew how.
C# 4.0 will have that feature.
It will be enabled on interfaces and delegates which adhere to certain rules.
List<T>won’t be supported, as it is a concrete class.IList<T>also won’t be supported, as its methods useTin both theinandoutpositions.