Using generics in abstract classes

  • You need to declare the type T.
  • You need to declare the class AbstractClass as abstract.
  • You need to use the override keyword.

Try this:

public class Widgets { }

public abstract class AbstractClass<T>
{
    public int Id { get; set; }
    public int Name { get; set; }

    public abstract List<T> Items { get; set; }
}

public class Container : AbstractClass<Widgets>
{
    public override List<Widgets> Items { get; set; }
}

  1. You need to mark AbstractClass abstract, because it contains abstract property

  2. Specify the generic type in the AbstractClass declaration

  3. Implement abstract property with override


public abstract class AbstractClass<T>
{
    public int Id { get; set; }
    public int Name { get; set; }

    public abstract List<T> Items { get; set; }
}

public class Container : AbstractClass<Widgets>
{
    public override List<Widgets> Items { get; set; }
}


You need the declaration on the class as well, to know what type T is:

public abstract class AbstractClass<T>
{
    public int Id { get; set; }
    public int Name { get; set; }

    public abstract List<T> Items { get; set; }
}

public class Container : AbstractClass<Widgets>
{
    public override List<Widgets> Items { get; set; }
}

You can also restrict what T can be, like say it must implement IWidgets:

public class AbstractClass<T> where T : IWidgets

You need to make AbstractClass generic

public class AbstractClass<T> {
  ...
}

public class Container : AbstractClass<Widgets> { ...
}