title: Should an IEnumerable iterator on a Queue dequeue an item
body: <p>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.</p>
  <pre><code>public interface IQueue&lt;TQueueItem&gt;
  {
      void Enqueue(TQueueItem queueItem);
      TQueueItem Dequeue();
  }
  
  public class CustomQueue&lt;TQueueItem&gt; : IQueue&lt;TQueueItem&gt;
  {
      private readonly Queue&lt;TQueueItem&gt; queue = new Queue&lt;TQueueItem&gt;();
      ...
      public void Enqueue(TQueueItem queueItem)
      {
          ...
          queue.Enqueue( queueItem );
          ...
      }
  
      public TQueueItem Dequeue()
      {
          ...
          return queue.Dequeue();
          ...
      }
  }
  </code></pre>
  <p>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.</p>
  <p>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.</p>
  <pre><code>public class CustomQueue&lt;TQueueItem&gt; : IQueue&lt;TQueueItem&gt;, IEnumerable&lt;TQueueItem&gt;
  {
      ...
  
      public IEnumerator&lt;TQueueItem&gt; GetEnumerator()
      {
          while (queue.Count &gt; 0)
          {
              yield return Dequeue();
          }
      }
  
      //Or
  
      public IEnumerator&lt;TQueueItem&gt; GetEnumerator()
      {
          return queue.GetEnumerator();
      }
  
      ...
  }
  </code></pre>
  <p>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.</p>
  <p>EDIT</p>
  <p>To put things into context. The class that I am implementing does a Monitor.Wait when Dequeuing and there are no items in the queue. When an item is put onto the queue there is a Monitor.Pulse. This allows for one thread to push stuff onto the queue and the other to essentially &quot;watch&quot; the queue.</p>
  <p>From a coding point of view I am trying to decide which it looks cleaner:</p>
  <pre><code>foreach(QueueItem item in queue)
  {
      DoSomethingWithThe(item);
  }
  
  //Or
  
  while(systemIsRunning)
  {
      DoSomethingWithThe(queue.Dequeue());
  }
  </code></pre>
  <p>For my particular implementation it wouldn't matter if there were multiple process dequeuing items. Because it is a queue they can both pick of an item as no item should be processed more than once, hence the use of a queue.</p>
  <p>EDIT</p>
  <p>Interestingly enough I have found a blog post where someone has done exactly this.</p>
  <p><a href="https://docs.microsoft.com/en-us/archive/blogs/toub/blocking-queues" rel="nofollow noreferrer">Link</a></p>
  <p>EDIT</p>
  <p>One last stab at this before I close this off. How do people feel about the class not implement IEnumerable but having an IEnumerator GetEnumerator() method that dequeues items? The .net language supports duck typing, foreach being one of the uses. Perhaps this deserves it's own question?</p>
  <p>EDIT</p>
  <p>Have raised the question of implementing a GetEnumerator method without implementing IEnumerable in another <a href="https://stackoverflow.com/questions/4194900/should-a-getenumerator-method-still-be-idempotent-when-the-class-does-not-implem">question</a>.</p>