Limit dimension of Queue <T> in.NET?

I have a Queue object that I have actually initialised to an ability of 2, yet clearly that is simply the ability and also it maintains increasing as I add things. Exists currently an object that instantly dequeues a thing when the restriction is gotten to, or is the most effective remedy to create my very own acquired class?

0
2019-05-07 11:59:25
Source Share
Answers: 3

Why would not you simply make use of an array with a dimension of 2? A Queue is intended to be able to dynamically expand and also reduce.

Or create a wrapper class around an instance of Queue<T> instance and also each time one enqueues a <T> object, examine the dimension of the queue. If bigger than 2, dequeue the first thing.

0
2019-05-09 05:50:50
Source

You need to create your very own class, a ringbuffer would possibly fit your demands.

The information frameworks in.NET that permits you to define ability, with the exception of array, utilizes this to construct the inner data structure made use of to hold the inner information.

As an example, for a checklist, ability is made use of to size an inner array. When you start including components to the checklist, it'll start loading this array from index 0 and also up, and also when it reaches your ability, it raises the ability to a new greater ability, and also proceeds loading it up.

0
2019-05-07 19:43:58
Source

I've knocked up a standard variation of what I'm seeking, it's not excellent yet it'll get the job done till something much better comes.

public class LimitedQueue<T> : Queue<T>
{
    public int Limit { get; set; }

    public LimitedQueue(int limit) : base(limit)
    {
        Limit = limit;
    }

    public new void Enqueue(T item)
    {
        while (Count >= Limit)
        {
            Dequeue();
        }
        base.Enqueue(item);
    }
}
0
2019-05-07 16:50:56
Source