Overriding List's Add()

后端 未结 6 1976
陌清茗
陌清茗 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:20

    If you want a collection in which you'd want to override your Add/Insert or Indexer then you could create your own collection that would inherit from:

    Collection
    

    and override the following methods:

    InsertItem();
    SetItem();
    

    the first one is called by Add() and Insert(), while the second is called by this[]

    The List Add can not be overriden since the collection is made for perfomance, thus the overriding possibility is intentionally removed

提交回复
热议问题