Get or set the number of elements in the BitArray in C#

The BitArray.Length property in C# gets or sets the number of elements in the BitArray. This property is essential for determining the size of a BitArray and can also be used to resize it dynamically.

Syntax

Following is the syntax for getting the length of a BitArray −

int length = bitArray.Length;

Following is the syntax for setting the length of a BitArray −

bitArray.Length = newLength;

Getting BitArray Length

The Length property returns the total number of elements in the BitArray, regardless of their values −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr1 = new BitArray(3);
        BitArray arr2 = new BitArray(5);
        
        arr1[0] = false;
        arr1[1] = true;
        arr1[2] = false;
        
        Console.WriteLine("BitArray1 length = " + arr1.Length);
        Console.WriteLine("Elements in BitArray1...");
        foreach (bool res in arr1) {
            Console.WriteLine(res);
        }
        
        Console.WriteLine("\nBitArray2 length = " + arr2.Length);
        Console.WriteLine("Elements in BitArray2...");
        foreach (bool res in arr2) {
            Console.WriteLine(res);
        }
    }
}

The output of the above code is −

BitArray1 length = 3
Elements in BitArray1...
False
True
False

BitArray2 length = 5
Elements in BitArray2...
False
False
False
False
False

Setting BitArray Length

You can modify the size of a BitArray by setting its Length property. When increasing the length, new elements are initialized to false. When decreasing, elements beyond the new length are removed −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr = new BitArray(3);
        arr[0] = true;
        arr[1] = false;
        arr[2] = true;
        
        Console.WriteLine("Original BitArray length = " + arr.Length);
        Console.WriteLine("Elements:");
        foreach (bool res in arr) {
            Console.WriteLine(res);
        }
        
        // Increase length
        arr.Length = 5;
        Console.WriteLine("\nAfter increasing length to 5:");
        Console.WriteLine("BitArray length = " + arr.Length);
        foreach (bool res in arr) {
            Console.WriteLine(res);
        }
        
        // Decrease length
        arr.Length = 2;
        Console.WriteLine("\nAfter decreasing length to 2:");
        Console.WriteLine("BitArray length = " + arr.Length);
        foreach (bool res in arr) {
            Console.WriteLine(res);
        }
    }
}

The output of the above code is −

Original BitArray length = 3
Elements:
True
False
True

After increasing length to 5:
BitArray length = 5
True
False
True
False
False

After decreasing length to 2:
BitArray length = 2
True
False

Comparing BitArray Lengths

The Length property is useful when comparing BitArrays or performing operations that depend on size −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr1 = new BitArray(3);
        BitArray arr2 = new BitArray(3);
        BitArray arr3 = new BitArray(5);
        
        arr1[0] = true; arr1[1] = false; arr1[2] = true;
        arr2[0] = true; arr2[1] = false; arr2[2] = true;
        
        Console.WriteLine("BitArray1 length = " + arr1.Length);
        Console.WriteLine("BitArray2 length = " + arr2.Length);
        Console.WriteLine("BitArray3 length = " + arr3.Length);
        
        Console.WriteLine("\nSame length (arr1 vs arr2): " + (arr1.Length == arr2.Length));
        Console.WriteLine("Same length (arr1 vs arr3): " + (arr1.Length == arr3.Length));
        
        // Note: BitArray.Equals compares references, not content
        Console.WriteLine("Reference equality: " + arr1.Equals(arr2));
    }
}

The output of the above code is −

BitArray1 length = 3
BitArray2 length = 3
BitArray3 length = 5

Same length (arr1 vs arr2): True
Same length (arr1 vs arr3): False
Reference equality: False

Conclusion

The BitArray.Length property provides both read and write access to the number of elements in a BitArray. It allows you to determine the current size and dynamically resize the array as needed, with new elements defaulting to false when the array is expanded.

Updated on: 2026-03-17T07:04:36+05:30

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements