{"record":{"id":"7998482ab6344557","repo":"TheAlgorithms/C-Sharp","slug":"n","errorCode":null,"errorMessage":"n","messagePattern":"n","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/NQueens/BacktrackingNQueensSolver.cs","lineNumber":15,"sourceCode":"namespace Algorithms.Problems.NQueens;\n\npublic class BacktrackingNQueensSolver\n{\n    /// <summary>\n    ///     Solves N-Queen Problem given a n dimension chessboard and using backtracking with recursion algorithm.\n    ///     If we find a dead-end within or current solution we go back and try another position for queen.\n    /// </summary>\n    /// <param name=\"n\">Number of rows.</param>\n    /// <returns>All solutions.</returns>\n    public IEnumerable<bool[,]> BacktrackSolve(int n)\n    {\n        if (n < 0)\n        {\n            throw new ArgumentException(nameof(n));\n        }\n\n        return BacktrackSolve(new bool[n, n], 0);\n    }\n\n    private static IEnumerable<bool[,]> BacktrackSolve(bool[,] board, int col)\n    {\n        var solutions = col < board.GetLength(0) - 1\n            ? HandleIntermediateColumn(board, col)\n            : HandleLastColumn(board);\n        return solutions;\n    }\n\n    private static IEnumerable<bool[,]> HandleIntermediateColumn(bool[,] board, int col)\n    {\n        // To start placing queens on possible spaces within the board.\n        for (var i = 0; i < board.GetLength(0); i++)\n        {","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/NQueens/BacktrackingNQueensSolver.cs#L1-L33","documentation":"BacktrackSolve validates the board size before solving the N-Queens problem. It throws ArgumentException when n is negative, because a negative dimension cannot form a bool[n,n] board. The parameter name is passed as the 'message', so the error text is just 'n'.","triggerScenarios":"Calling BacktrackSolve with any negative integer, e.g. BacktrackSolve(-1) or a size computed from user input or a subtraction that underflows.","commonSituations":"User-supplied board size not validated upstream; computing n from a difference of lengths that came out negative; config default of -1 meaning 'unset' passed straight through.","solutions":["Pass a non-negative n (0 is allowed and yields an empty solution set).","Validate n >= 0 at the call site before invoking.","Fix the upstream calculation or config parsing that produced the negative value."],"exampleFix":"// before\nsolver.BacktrackSolve(-1);\n// after\nif (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, \"Must be >= 0\");\nvar solutions = solver.BacktrackSolve(n);","handlingStrategy":"validation","validationCode":"if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, \"Board size must be non-negative\");","typeGuard":"static bool IsValidBoardSize(int n) => n >= 0;","tryCatchPattern":null,"preventionTips":["Validate sizes at system boundaries before calling solver APIs","Avoid sentinel -1 values for 'unset' numeric config","Unit-test solvers with 0 and boundary sizes"],"tags":["argument-validation","csharp","algorithm"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}