C# - BitArray.Get() Method with Example

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

BitArray.Get() Method

The BitArray.Get() method is used to get the value of the bit at a specific position in the BitArray.


bool BitArray.Get(int index);


  • index: The index of the value to get.

Return Value

It returns a boolean value stored at the given index in the BitArray.


  • System.ArgumentOutOfRangeException

C# Example of BitArray.Get() Method

The source code to get the value of the bit at a specific position 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 object
    BitArray bitArr = new BitArray(5);

    int index = 0;

    bitArr[0] = true;
    bitArr[2] = true;
    bitArr[3] = true;

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


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

C# BitArray Class Programs »

Comments and Discussions!

Load comments ↻

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