How to I apply moves (bitboard) and continue searching? My current attempt is throwing a strange error! Explanation
I have a list of all possible current moves in a position. At each node, I want to then generate all possible moves and continue. The problem is that my code is modifying the list of moves in the node above. At least I think this is the case!
I am open to completely changing the way I handle moves, as I feel this isn't very efficient since I have a struct for every move.
Output
This is the current output.
PS D:\Users\kmont\Desktop\ChessEngine&gt; dotnet run
Number of moves at depth 1 is 4. It is White's turn.
Number of moves at depth 2 is 4. It is Black's turn. Previous move was knight to a3
Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
    at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
    at System.Collections.Generic.List`1.Enumerator.MoveNext()
    at Program.search(Int32 depth, Board board, String prevMove) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 55
    at Program.search(Int32 depth, Board board, String prevMove) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 60
    at Program.Main(String[] args) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 33

Code
The Move Struct:
public struct Move{
    public Piece.Type pieceType;
    public UInt64 piece;
    public UInt64 moveTo;
    public int color;

    public bool quiet;
    public UInt64 capturedPiece;
    public int capturedColor;
}

Search function:
public static void search(int depth, Board board, string prevMove = &quot;nothing&quot;){

    // If at the end of the tree, return.

    depth++;
    if (depth == maxDepth) return;

    Console.Write(&quot;Number of moves at depth {0} is {1}. It is {2}'s turn.&quot;, depth, board.allMoves.Count, board.color == 0 ? &quot;White&quot; : &quot;Black&quot;);
    if (prevMove != &quot;nothing&quot;) Console.WriteLine(&quot; Previous move was knight to &quot; + prevMove);
    else Console.WriteLine(&quot;&quot;);

    // Uncomment below line for a list of all possible moves.
    //if (true) foreach (Board.Move move in board.allMoves) Console.WriteLine(String.Format(&quot;    {0} {1} on square {2} can move to {3}.&quot;, move.color == 0 ? &quot;White&quot; : &quot;Black&quot;, move.pieceType.ToString(), help.square(move.piece), help.square(move.moveTo)));

    foreach (Board.Move move in board.allMoves){
        Board newBoard = board;
        newBoard.color = 1 - board.color;
        initialiseBoard(newBoard);
        newBoard = makeMove(newBoard, move);
        search(depth, newBoard, help.square(move.moveTo));
    }
}

Make move function:
public static Board makeMove(Board board, Board.Move move){

    string moveColor = move.color == 0 ? &quot;White&quot; : &quot;Black&quot;;
    string boardColor = board.color == 0 ? &quot;White&quot; : &quot;Black&quot;;
    string pmove = help.square(move.moveTo);
    //Console.WriteLine(String.Format(&quot;    Making {0} move {2}: It is {1}'s turn.&quot;, moveColor, boardColor, pmove));
    
    Board toReturn = board;
    // Remove piece from board
    if (toReturn.color == 0 &amp; move.color == 0){
        if (move.pieceType == Piece.Type.Knight){
            toReturn.WhiteKnightsBitboard &amp;= ~move.piece; // Get rid of piece on board
            toReturn.WhiteKnightsBitboard |= move.moveTo; // Add piece to board
        }
    }
    else if (toReturn.color == 1 &amp; move.color == 1){
        if (move.pieceType == Piece.Type.Knight){
            toReturn.BlackKnightsBitboard &amp;= ~move.piece;
            toReturn.BlackKnightsBitboard |= move.moveTo;
        }
    }

    Game.node++; // To count nodes
    return toReturn;

}

Initialise Board function:
static void initialiseBoard(Board board){
    BoardOperations.updatePieces(board); // Sets all pieces and finds empty squares 
    board.generateAttackSquares(board);  // Finds white knight moves and puts them into board.allMoves
}
The issue may be here:
Board newBoard = board;

This creates a new variable, but it doesn't actually create a new Board. board and newBoard are pointing to the exact same Board object. So, when your code changes anything in newBoard, it also changes it in board, and when it changes, you get this error.
What you may want to do is make an actual copy of board, so when you change it, it doesn't change the original.