TheAlgorithms/C-Sharp · error · ArgumentException

Sequence must been greater than or equal to 1

Error message

Sequence must been greater than or equal to 1

What it means

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).

Solutions

  1. Validate string.IsNullOrEmpty before constructing and provide a default or error path
  2. Wrap construction in try/catch for ArgumentException when input length is not guaranteed
  3. Fix upstream so an empty string never reaches the constructor (required-field validation)
  4. Use the length-based constructor `new BitArray(int length)` when you only need an array of a given size

Example fix

// before
var bits = new BitArray(config.BitString); // throws if empty
// after
if (string.IsNullOrEmpty(config.BitString))
{
    throw new InvalidOperationException("BitString is required");
}
var bits = new BitArray(config.BitString);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(sequence)) throw new ArgumentException("Bit sequence is required"); var bits = new BitArray(sequence);

Type guard

static bool IsValidBitString(string s) => !string.IsNullOrEmpty(s) && s.All(c => c == '0' || c == '1');

Try / catch

try { var bits = new BitArray(sequence); } catch (ArgumentException) { /* empty or invalid sequence */ }

Prevention

When it happens

Trigger: `new BitArray("")` — constructing from an empty string, typically from an unset config value, empty environment variable, or a parsing function that returned string.Empty.

Common situations: 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.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/83015d8344f3ab60. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/BitArray.cs:154

    public BitArray(int n)
    {
        field = n <= 0 ? new bool[0] : new bool[n];
    }

    /// <summary>
    ///     Initializes a new instance of the <see cref="BitArray" /> class.
    ///     Setups the array with the input sequence.
    ///     purpose: Setups the array with the input sequence.
    ///     assumes: sequence must been greater or equal to 1.
    ///     the sequence may only contain ones or zeros.
    /// </summary>
    /// <param name="sequence">A string sequence of 0's and 1's.</param>
    public BitArray(string sequence)
    {
        // precondition I
        if (sequence.Length <= 0)
        {
            throw new ArgumentException("Sequence must been greater than or equal to 1");
        }

        // precondition II
        ThrowIfSequenceIsInvalid(sequence);

        field = new bool[sequence.Length];
        Compile(sequence);
    }

    /// <summary>
    ///     Initializes a new instance of the <see cref="BitArray" /> class.
    ///     Setups the bit-array with the input array.
    /// </summary>
    /// <param name="bits">A boolean array of bits.</param>
    public BitArray(bool[] bits) => field = bits;

    /// <summary>
    ///     Gets the length of the current bit array.

View on GitHub (pinned to 96e2905cab)