Using generics in abstract classes

前端 未结 7 1525
眼角桃花
眼角桃花 2020-12-05 10:17

I\'m working on an abstract class where the implementing class needs to implement a list of T. The problem is that this doesn\'t work:

public class AbstractC         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-05 11:05

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

    public abstract class AbstractClass
    {
        public int Id { get; set; }
        public int Name { get; set; }
    
        public abstract List Items { get; set; }
    }
    
    public class Container : AbstractClass
    {
        public override List Items { get; set; }
    }
    

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

    public class AbstractClass where T : IWidgets
    

提交回复
热议问题