Question
I have a Queue<T> object that I have initialised to a capacity of 2, but obviously that is just the capacity and it keeps expanding as I add items. Is there already an object that automatically dequeues an item when the limit is reached, or is the best solution to create my own inherited class?
Answer
I've knocked up a basic version of what I'm looking for, it's not perfect but it'll do the job until something better comes along.
public class LimitedQueue<T> : Queue<T>
{
private int limit = -1;
public int Limit
{
get { return limit; }
set { limit = value; }
}
public LimitedQueue(int limit)
: base(limit)
{
this.Limit = limit;
}
public new void Enqueue(T item)
{
if (this.Count >= this.Limit)
{
this.Dequeue();
}
base.Enqueue(item);
}
}
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/1292/" >Limit size of Queue
0 comments:
Post a Comment