title: Dequeue in a Priority queue using a Sorted Dictionary in C#
body: <p>I have a sorted dictionary of the form:</p>
  
  <pre><code>SortedDictionary&lt;PriorityType, List&lt;T&gt;&gt; dictionary;
  </code></pre>
  
  <p>where PriorityType is an enum class.</p>
  
  <p>Now I am trying to make Priority queue methods but I have apprehensions about whether my Dequeue method will work or not.</p>
  
  <pre><code>public T Dequeue()
      {
          if (IsEmpty())
          {
              throw new Exception("The Priority Queue is empty! Dequeuing is not possible!");
          }
  
          var highestPriorityList = dictionary[dictionary.Keys.First()];
          var topElement = highestPriorityList.FirstOrDefault();
          if (highestPriorityList.Count == 0)
          {
              dictionary.Remove(dictionary.Keys.First());
          }
          highestPriorityList.RemoveAt(0);
          return topElement;
      }
  </code></pre>
  
  <p>Please help me to improve this method!</p>
  
  <p>Note: The Dequeue() method is supposed to Remove and return the object with the highest priority and which is before other elements with same priority.</p>