Limit dimension of Queue <T> in.NET?
I have a Queue
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.
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.
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);
}
}
Related questions