If I have a root interface
public interface IEntity {}
And a derived interface, and classes:
public interface IFruit : IEntity {}
public class Apple : IFruit {}
public class Orange: IFruit {}
And, irrelevant, but perhaps others that don't implement IFruit
:
public class Computer : IEntity {}
And a generic class that uses all this:
public class PurchasedItem<T> where T : IFruit
{
public int Qty{get;set;}
public T Item{get;set;}
}
How can I declare a list that contains PurchasedItem<IFruit>
and work with it?
If I do this:
var list = new List<PurchasedItem<IFruit>>();
list.Add(new PurchasedItem<Apple>());
...then I get an error
Cannot convert from PurchasedItem<Apple> to PurchasedItem<IFruit>