I am trying to get to grips with more complicated inheritance structures and generics and I am trying to create some architecture for a current project which is following this suit. My problem currently is I am getting this error:
Type argument ‘Foo’ does not inherit from or implement the constraint type ‘ListBase’
public class ItemBase {}
public class ListBase<T> where T : ItemBase
{
public virtual List<T> ListExample {get; set; }
}
These are my base classes, although they probably aren’t named appropriately I have just tried to show a simple example of what I am trying to achieve.
public class FooItem : ItemBase { }
public class Foo : ListBase<FooItem>
{
public override List<FooItem> ListExample { get; set;}
}
So I can then extend the initial base class for the lists and do more with it, but I want a generic way of handling all of these classes.
public class ListHandler<T> where T : ListBase<ItemBase> { }
When I try to pass Foo as T to the ListHandler I get the error mentioned, I thought that inevitably because Foo is a List<ItemBase> and FooItem is of type ItemBase I would be able to do this var handler = new ListHandler<Foo>();.
Could anybody explain why I can’t do this or what I am doing wrong?
A
ListBase<ItemBase>is not the same as aListBase<FooItem>.In particular, you can add any kind of
ItemBaseto aListBase<ItemBase>.You need to accept two generic parameters: