In the following example, why can’t I cast collectionA to collectionB given that the compiler knows that a TItem is a A<T>?
public class A<T>
{
}
public void Foo<TItem, T> () where TItem : A<T>
{
var collectionA = new List<TItem>();
var collectionB = (List<A<T>>)collectionA; // "Cannot cast" error here
}
The problem is that it would allow you to place inappropriate items into collectionA.
Here’s a simplified reworking of it, which hopefully makes it easier to see the problem:
Suppose you have (pseudocode):
Now imagine you could do this:
Then you would be able to do this: