Overriding List's Add()

后端 未结 6 1965
陌清茗
陌清茗 2020-12-20 18:59

Can\'t i overload List\'s Add method ?

class ListDemo:List
 {
    public override T  Add(T value)
   {
      return base.Add(valu         


        
6条回答
  •  攒了一身酷
    2020-12-20 19:37

    This worked for me (based on Sam Harwell's comment):

    public class ListDemo : Collection
    {
        protected override void InsertItem(int index, T item)
        {
            // Your add condition ...
            if (!this.Any(x => ...))
                base.InsertItem(index, item);
        }
    }
    

提交回复
热议问题