{"record":{"id":"83015d8344f3ab60","repo":"TheAlgorithms/C-Sharp","slug":"sequence-must-been-greater-than-or-equal-to-1","errorCode":null,"errorMessage":"Sequence must been greater than or equal to 1","messagePattern":"Sequence must been greater than or equal to 1","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/BitArray.cs","lineNumber":154,"sourceCode":"    public BitArray(int n)\n    {\n        field = n <= 0 ? new bool[0] : new bool[n];\n    }\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"BitArray\" /> class.\n    ///     Setups the array with the input sequence.\n    ///     purpose: Setups the array with the input sequence.\n    ///     assumes: sequence must been greater or equal to 1.\n    ///     the sequence may only contain ones or zeros.\n    /// </summary>\n    /// <param name=\"sequence\">A string sequence of 0's and 1's.</param>\n    public BitArray(string sequence)\n    {\n        // precondition I\n        if (sequence.Length <= 0)\n        {\n            throw new ArgumentException(\"Sequence must been greater than or equal to 1\");\n        }\n\n        // precondition II\n        ThrowIfSequenceIsInvalid(sequence);\n\n        field = new bool[sequence.Length];\n        Compile(sequence);\n    }\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"BitArray\" /> class.\n    ///     Setups the bit-array with the input array.\n    /// </summary>\n    /// <param name=\"bits\">A boolean array of bits.</param>\n    public BitArray(bool[] bits) => field = bits;\n\n    /// <summary>\n    ///     Gets the length of the current bit array.","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/BitArray.cs#L136-L172","documentation":"BitArray's string constructor validates the input sequence and throws ArgumentException when the string is empty (Length <= 0). A bit array must contain at least one bit, so an empty string has no valid representation. This is the first of two precondition checks (the second validates the characters).","triggerScenarios":"`new BitArray(\"\")` — constructing from an empty string, typically from an unset config value, empty environment variable, or a parsing function that returned string.Empty.","commonSituations":"Missing config/CLI input where a bit string was expected; a regex/parse step producing an empty match; deserializing empty fields from JSON/CSV into BitArray.","solutions":["Validate string.IsNullOrEmpty before constructing and provide a default or error path","Wrap construction in try/catch for ArgumentException when input length is not guaranteed","Fix upstream so an empty string never reaches the constructor (required-field validation)","Use the length-based constructor `new BitArray(int length)` when you only need an array of a given size"],"exampleFix":"// before\nvar bits = new BitArray(config.BitString); // throws if empty\n// after\nif (string.IsNullOrEmpty(config.BitString))\n{\n    throw new InvalidOperationException(\"BitString is required\");\n}\nvar bits = new BitArray(config.BitString);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(sequence)) throw new ArgumentException(\"Bit sequence is required\"); var bits = new BitArray(sequence);","typeGuard":"static bool IsValidBitString(string s) => !string.IsNullOrEmpty(s) && s.All(c => c == '0' || c == '1');","tryCatchPattern":"try { var bits = new BitArray(sequence); } catch (ArgumentException) { /* empty or invalid sequence */ }","preventionTips":["Validate config/CLI inputs for required non-empty bit strings","Combine length and character validation before construction","Default missing values explicitly instead of passing string.Empty"],"tags":["csharp","bitarray","empty-input"],"backgroundTag":"empty-required-field","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"}