Maximum capacity collection in c#

前端 未结 9 2002
Happy的楠姐
Happy的楠姐 2020-12-09 04:27

In the .Net BCL is there a collection data structure similar to list that has a maximum capacity, say configured to 100 items, which when item 101 is added the original firs

9条回答
  •  攒了一身酷
    2020-12-09 05:23

    you can also just override Collection

    public class FixedSizeList : Collection
    {
        public int MaxItems {get;set;}
    
        protected override void InsertItem(int index, T item){
            base.InsertItem(index, item);
    
            while (base.Count > MaxItems) {
                base.RemoveItem(0);
            }
        }
    }
    

提交回复
热议问题