Fixed-size Queue?  
Author Message

PostPosted: .NET Base Class Library, Fixed-size Queue? Top

I'm in need of a fixed size Queue. According to the msdn documentation, a Queue object will grow as more items are inserted into it. I need a Queue that operates more like (in real-world terms) a length of pipe. You can insert a fixed number of tennis balls into a pipe, and then they start coming out the other side. All the while, the pipe only holds a certain number of tennis balls.
A situation like this would work...
Queue<TennisBall> q = new Queue<TennisBall>(MAX_TENNIS_BALLS);
if (q.Count == MAX_TENNIS_BALLS) {
} else {
...if I could be guaranteed that the Queue object would stay the same size. Obviously I am not concerned about the tennis balls that get dequeued - only the ones in the Queue matter. Any suggestions as to how I could accomplish this would be appreciated.
Thanks in advance!

.NET Development2  
James Curran

PostPosted: .NET Base Class Library, Fixed-size Queue? Top

Well, since generally, people are concerned about the ones that get dequeued, it's doubtful you're going to find a pre-written class to do what you want. However, creating your own should be that much trouble.

Inherit MyQueue for Queue:

public class MyQueue<T> : Queue<T>

or my may even want to go:

public class TennisBallQueue : Queue<TennisBall>

Then just override the Enqueue method to do the size check:

public new void Enqueue(TennisBall tb)
if (this.Count == MAX_TENNIS_BALLS)


(Unfortuantely the Enqueue is not virtual in the generic Queue<> class)


PostPosted: .NET Base Class Library, Fixed-size Queue? Top

Forgot to follow up on this one. I found an implementation of a cyclic queue on CodeProject that worked for me.