Limit the size of a generic collection?

后端 未结 4 1022
野的像风
野的像风 2020-12-31 18:20

Is there any way to limit the size of a generic collection?

I have a Stack of WriteableBitmap which I am using to store a clone of a WriteableBitmap on each change,

4条回答
  •  灰色年华
    2020-12-31 18:58

    You have to implement your own wrapper to achieve that. There is no direct option available.

    class FixedSizeStack : Stack
    {
        private int MaxNumber;
        public FixedSizeStack(int Limit)
            : base()
        {
            MaxNumber = Limit;
        }
    
        public override void Push(object obj)
        {
            if (this.Count < MaxNumber)
                base.Push(obj);
        }
    
    }
    

提交回复
热议问题