// The Structure of the Container and the items
public interface IContainer where TItem : IItem
{
}
public class AContainer : IContainer
Another generics covariant problem...
Generic types in .NET are not covariant or contravariant - IContainer<ItemA> (which is what AContainer is) is not a subclass of IContainer<IItem> - there is no valid cast between the two. This will be fixed in C# 4.
If you want to use AContainer
to be an IContainer<IItem>
, you need to implement this interface as well:
public class AContainer : IContainer<ItemA>, IContainer<IItem>
You may implement it explicitly.
You can also consider Simulated Covariance for .NET Generics by Krzysztof Cwalina