简体   繁体   English

C#在数组列表上使用foreach时,即使列表长度为86,foreach也仅迭代5次

[英]C# When using foreach on a list of arrays, the foreach only iterates 5 times even though the list has length 86

I'm writing a chess engine in C#, and I'm trying to debug move generation. 我正在用C#编写象棋引擎,并且试图调试移动生成。 I've been using breakpoints so far to check variables, but I need a better way to debug. 到目前为止,我一直在使用断点来检查变量,但是我需要一种更好的调试方法。 Despite the fact that a breakpoint shows that this list has a length of 86, the loop only runs 5 times. 尽管断点表明该列表的长度为86,但循环仅运行5次。 The only thing I can think of is that the arrays in the list have a length of 5, but I have no idea what would be causing this. 我唯一能想到的是列表中的数组长度为5,但是我不知道会导致什么。

foreach (int[] debugBoard in futures)
{

    for (int i = 0; i < 5; i++)
    {
        Debug.Write(debugBoard[i].ToString().PadLeft(3, ' '));
    }
    Debug.Write("\n");
    int[,] debugOutBoard = new int[8, 8];
    Array.Copy(chessBoard.Pieces(), debugOutBoard, 64);

    if (debugBoard[0] < 0 || debugBoard[1] < 0 || debugBoard[2] < 0 || debugBoard[3] < 0 || debugBoard[0] > 7 || debugBoard[1] > 7 || debugBoard[2] > 7 || debugBoard[3] > 7) 
        break;

    debugOutBoard[debugBoard[2], debugBoard[3]] = debugOutBoard[debugBoard[0], debugBoard[1]];
    debugOutBoard[debugBoard[0], debugBoard[1]] = 0;
    int rowLength = debugOutBoard.GetLength(0);
    int colLength = debugOutBoard.GetLength(1);

    for (int i = 0; i < rowLength; i++)
    {
        for (int j = 0; j < colLength; j++)
        {
            Debug.Write(debugOutBoard[i, j].ToString().PadLeft(3, ' '));
        }
        Debug.Write(Environment.NewLine + Environment.NewLine);
    }
}

Also, I tried using a concurrentbag to store moves in (I was going to multithread the move processing later on) but as soon as the foreach loop touched the concurrent bag, all the memory values of the bag changed to one value. 另外,我尝试使用并发包来存储移动(稍后我将对移动处理进行多线程处理),但是一旦foreach循环触及并发包时,该包的所有内存值都更改为一个值。 I've been stuck on this roadblock for days, and I really need help. 我被困在这个障碍上已经好几天了,我真的需要帮助。

Your if statement breaks out of the for loop when its condition is met, I presume this happens for the first time on the 5th/6th iteration. 您的if语句在满足其条件时会中断for循环,我认为这是在第5/6次迭代中第一次发生。

What I meant to do is iterate to the next element. 我的意思是迭代到下一个元素。 What command would I use to do that? 我将使用什么命令来执行此操作? -

You need to use continue instead of break 您需要使用continue而不是break

If 'futures' contains 86 items the only way it could stop iterating is if an exception occurs. 如果'futures'包含86个项目,则唯一可以停止迭代的方法是发生异常。 Visual studio (In default settings) should break when this occurs unless you handle the exception somewhere. 除非您在某处处理异常,否则Visual Studio(默认设置)应该在发生这种情况时中断。

Wrap the whole thing in a try{} catch{} and set a breakpoint in catch and see if it hits. 将整个内容包装在try {} catch {}中,并在catch中设置一个断点,看看是否命中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM