Priority Queue in C# I develop a small game using C# and need A* in it. For that, I need a PriorityQueue, which .NET does not have. I wanted to make my own one for practice. Here is it, please comment on performance and usability:

public class PriorityQueue&lt;T&gt; : IEnumerable
{
    List&lt;T&gt; items;
    List&lt;int&gt; priorities;

    public PriorityQueue()
    {
        items = new List&lt;T&gt;();
        priorities = new List&lt;int&gt;();
    }

    public IEnumerator GetEnumerator() { return items.GetEnumerator(); }
    public int Count { get { return items.Count; } }

    /// &lt;returns&gt;Index of new element&lt;/returns&gt;
    public int Enqueue(T item, int priority)
    {
        for (int i = 0; i &lt; priorities.Count; i++) //go through all elements...
        {
            if (priorities[i] &gt; priority) //...as long as they have a lower priority.    low priority = low index
            {
                items.Insert(i, item);
                priorities.Insert(i, priority);
                return i;
            }
        }

        items.Add(item);
        priorities.Add(priority);
        return items.Count - 1;
    }

    public T Dequeue()
    {
        T item = items[0];
        priorities.RemoveAt(0);
        items.RemoveAt(0);
        return item;
    }

    public T Peek()
    {
        return items[0];
    }

    public int PeekPriority()
    {
        return priorities[0];
    }
}


I tested it with...

        PriorityQueue&lt;String&gt; pQ = new PriorityQueue&lt;string&gt;();

        for (int i = 0; i &lt; 100; i++)
        {
            int prio = Provider.Rnd.Next(0, 1000);
            pQ.Enqueue(prio.ToString(), prio);
            if (pQ.Count &gt; 0 &amp;&amp; Provider.Rnd.Next(0, 2) == 0) pQ.Dequeue();
        }

        while (pQ.Count &gt; 0)
        {
            Console.WriteLine(pQ.Dequeue());
        }
There are other data structures for priority queues. You might consider implementing the queue as a binary heap instead, which gives you a run-time complexity of O(1) for accessing the "largest" (or "smallest", depending on the comparison) element, and O(log n) for insertion and removal (of the largest).