C# - BitArray.Length Property with Example

In this tutorial, we will learn about the C# BitArray.Length property with its definition, usage, syntax, and example. By Nidhi Last updated : March 31, 2023

BitArray.Length Property

The BitArray.Length is a property that is used to set or get the length of elements of an object of BitArray class.

Syntax

int BitArray.Length

Parameter(s)

  • None

Return Value

Returns an 'int' value.

Exception(s)

  • System.ArgumentOutOfRangeException

C# Example of BitArray.Length Property

The source code to get or set the number of elements in the BitArray is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

class BitArrayEx {
  //Entry point of Program
  static public void Main() {
    //Creation of BitArray objects
    BitArray bitArr = new BitArray(5);

    int index = 0;

    bitArr[0] = true;
    bitArr[1] = false;
    bitArr[2] = true;
    bitArr[3] = false;
    bitArr[4] = true;

    Console.WriteLine("Elements of BitArray:");
    for (index = 0; index < bitArr.Length; index++) {
      Console.WriteLine("\tIndex " + index + ": " + bitArr.Get(index));
    }

    bitArr.Length = 3;

    Console.WriteLine("Elements of BitArray:");
    for (index = 0; index < bitArr.Length; index++) {
      Console.WriteLine("\tIndex " + index + ": " + bitArr.Get(index));
    }
  }
}

Output

Elements of BitArray:
        Index 0: True
        Index 1: False
        Index 2: True
        Index 3: False
        Index 4: True
Elements of BitArray:
        Index 0: True
        Index 1: False
        Index 2: True
Press any key to continue . . .

C# BitArray Class Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.