小编Dav*_*oni的帖子

在While循环中使用Yield(PriorityQueue实现)

我最近一直在玩各种类型的PriorityQueue类,我遇到过一些我不太了解的行为.

这是我正在运行的单元测试的片段:

        PriorityQueue<Int32> priorityQueue = new PriorityQueue<Int32>();
        Randomizer r = new Randomizer();
        priorityQueue.AddRange(r.GetInts(Int32.MinValue, Int32.MaxValue, r.Next(300, 10000)));

        priorityQueue.PopFront(); // Gets called, and works correctly

        Int32 numberToPop = priorityQueue.Count / 3;
        priorityQueue.PopFront(numberToPop); // Does not get called, an empty IEnumberable<T> (T is an Int32 here) is returned
Run Code Online (Sandbox Code Playgroud)

正如我在评论中所指出的那样,PopFront()被调用并正确运行,但是当我尝试调用PopFront(numberToPop)时,该方法根本没有被调用,因为它甚至没有进入该方法.

以下是方法:

    public T PopFront()
    {
        if (items.Count == 0)
        {
            throw new InvalidOperationException("No elements exist in the queue");
        }

        T item = items[0];
        items.RemoveAt(0);
        return item;
    }

    public IEnumerable<T> PopFront(Int32 numberToPop)
    {
        Debug.WriteLine("PriorityQueue<T>.PopFront({0})", numberToPop); …
Run Code Online (Sandbox Code Playgroud)

.net c# yield-return

0
推荐指数
1
解决办法
3045
查看次数

标签 统计

.net ×1

c# ×1

yield-return ×1