KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have created a custom generic queue which implements a generic IQueue interface, which uses the generic Queue from the System.Collections.Generic namespace as a private inner queue. Example has been cleaned of irrelevant code. public interface IQueue<TQueueItem> { void Enqueue(TQueueItem queueItem); TQueueItem Dequeue(); } public class CustomQueue<TQueueItem> : IQueue<TQueueItem> { private readonly Queue<TQueueItem> queue = new Queue<TQueueItem>(); ... public void Enqueue(TQueueItem queueItem) { ... queue.Enqueue( queueItem ); ... } public TQueueItem Dequeue() { ... return queue.Dequeue(); ... } } I want to keep things consistent with the core implementations and have noticed that the core Queue implements IEnumerable so I will do the same either by explicitly implementing IEnumerable on the class or inheriting it with the IQueue interface. What I want to know is when enumerating over the queue should each move next dequeue the next item? I have used reflector to see how Microsoft has done it and all they do is step through the queues private array but Microsoft is far from infallible so I wanted to get a general opinion. public class CustomQueue<TQueueItem> : IQueue<TQueueItem>, IEnumerable<TQueueItem> { ... public IEnumerator<TQueueItem> GetEnumerator() { while (queue.Count > 0) { yield return Dequeue(); } } //Or public IEnumerator<TQueueItem> GetEnumerator() { return queue.GetEnumerator(); } ... } I am in two minds, on one hand I feel that iterating through a collection should not changed the collections state but on the other hand and especially with my particular implementation it would make the usage look clean. EDIT To put things into con
Tags (comma-separated)
Save Edits
Cancel