What is the Length property of BitArray class in C#?

The Length property of the BitArray class in C# is used to get or set the number of elements in the BitArray. This property allows you to determine the size of the bit array and also resize it when needed.

Syntax

Following is the syntax for using the Length property −

public int Length { get; set; }

Return Value

The Length property returns an int value representing the number of elements in the BitArray.

Using Length Property to Get BitArray Size

You can use the Length property to determine how many bits are stored in a BitArray

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        BitArray arr = new BitArray(5);
        Console.WriteLine("Count: {0}", arr.Count);
        Console.WriteLine("Length: {0}", arr.Length);
        
        // Set some values
        arr[0] = true;
        arr[2] = true;
        arr[4] = true;
        
        Console.WriteLine("BitArray elements:");
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("Index {0}: {1}", i, arr[i]);
        }
    }
}

The output of the above code is −

Count: 5
Length: 5
BitArray elements:
Index 0: True
Index 1: False
Index 2: True
Index 3: False
Index 4: True

Using Length Property to Resize BitArray

The Length property can also be used to resize an existing BitArray. When you increase the length, new elements are initialized to false. When you decrease the length, elements beyond the new size are removed −

using System;
using System.Collections;

public class ResizeDemo {
    public static void Main() {
        BitArray arr = new BitArray(3);
        arr[0] = true;
        arr[1] = false;
        arr[2] = true;
        
        Console.WriteLine("Original Length: {0}", arr.Length);
        Console.WriteLine("Original elements: {0}, {1}, {2}", arr[0], arr[1], arr[2]);
        
        // Increase the length
        arr.Length = 6;
        Console.WriteLine("\nAfter increasing length to 6:");
        Console.WriteLine("New Length: {0}", arr.Length);
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("Index {0}: {1}", i, arr[i]);
        }
        
        // Decrease the length
        arr.Length = 2;
        Console.WriteLine("\nAfter decreasing length to 2:");
        Console.WriteLine("New Length: {0}", arr.Length);
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("Index {0}: {1}", i, arr[i]);
        }
    }
}

The output of the above code is −

Original Length: 3
Original elements: True, False, True

After increasing length to 6:
New Length: 6
Index 0: True
Index 1: False
Index 2: True
Index 3: False
Index 4: False
Index 5: False

After decreasing length to 2:
New Length: 2
Index 0: True
Index 1: False

Length vs Count Property

Both Length and Count properties return the same value for BitArray, representing the number of elements. However, Length is specific to BitArray and allows resizing, while Count is inherited from ICollection and is read-only −

Property Access Purpose
Length Get/Set Get or set the number of elements, allows resizing
Count Get only Get the number of elements, inherited from ICollection

Conclusion

The Length property of BitArray class provides both read and write access to the size of the bit array. It allows you to determine the current number of elements and dynamically resize the array when needed, making it a versatile tool for managing bit collections.

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

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements