{"record":{"id":"0b1c819e46ca80d6","repo":"TheAlgorithms/C-Sharp","slug":"number-must-be-positive","errorCode":null,"errorMessage":"number must be positive","messagePattern":"number must be positive","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/BitArray.cs","lineNumber":566,"sourceCode":"\n        // actual compile procedure.\n        for (var i = 0; i < sequence.Length; i++)\n        {\n            field[i] = sequence[i] == '1';\n        }\n    }\n\n    /// <summary>\n    ///     Compiles integer number into the inner data structure.\n    ///     Assumes: the number must have the same bit length.\n    /// </summary>\n    /// <param name=\"number\">A positive integer number.</param>\n    public void Compile(int number)\n    {\n        // precondition I\n        if (number <= 0)\n        {\n            throw new ArgumentException($\"{nameof(number)} must be positive\");\n        }\n\n        // converts to binary representation\n        var binaryNumber = Convert.ToString(number, 2);\n\n        // precondition II\n        if (binaryNumber.Length > field.Length)\n        {\n            throw new ArgumentException(\"Provided number is too big\");\n        }\n\n        // for appropriate scaling\n        if (binaryNumber.Length < field.Length)\n        {\n            var difference = field.Length - binaryNumber.Length;\n            binaryNumber = new string('0', difference) + binaryNumber;\n        }\n","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/BitArray.cs#L548-L584","documentation":"BitArray.Compile(int) throws ArgumentException when the number is zero or negative. Only positive integers have a well-defined unsigned binary representation for this implementation, so the method rejects number <= 0 as its first precondition before converting to binary with Convert.ToString(number, 2).","triggerScenarios":"`bitArray.Compile(0)` or `bitArray.Compile(negativeInt)` — commonly from counter variables that start at 0, default/uninitialized int fields, or signed subtraction results.","commonSituations":"Passing an accumulator that never incremented; feeding a difference (a-b) that came out negative; default(struct) initialization yielding 0; parsing user input that permits zero.","solutions":["Validate `number > 0` before calling Compile and handle zero/negative explicitly","If zero must be representable, write the bits manually (e.g., compile the string \"0\") rather than using Compile(int)","Catch ArgumentException when caller-supplied values are untrusted","Clamp or map non-positive inputs to a valid domain before invoking"],"exampleFix":"// before\nbits.Compile(count); // throws when count == 0\n// after\nif (count > 0)\n{\n    bits.Compile(count);\n}\nelse\n{\n    bits.Compile(\"0\"); // explicit zero representation\n}","handlingStrategy":"validation","validationCode":"if (number > 0) bitArray.Compile(number); else /* handle zero/negative */;","typeGuard":"static bool IsCompilable(int n) => n > 0 && Convert.ToString(n, 2).Length <= bitArray.Length;","tryCatchPattern":"try { bitArray.Compile(number); } catch (ArgumentException) { /* non-positive or too big */ }","preventionTips":["Reject non-positive values at the input boundary","Initialize counters before compiling","Document Compile(int) as positive-only in callers"],"tags":["csharp","bitarray","invalid-argument"],"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"}