C# - BitArray.SetAll() Method with Example

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

BitArray.SetAll() Method

The BitArray.SetAll() method is used to set a specified boolean value to all elements of BitArray. The boolean value can be True or False.

Syntax

void BitArray.SetAll(bool value);

Parameter(s)

  • value: a Boolean value to be assigned to all bits.

Return Value

It does not return any value.

C# Example of BitArray.SetAll() Method

The source code to set all bits in the BitArray to the specified value 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;

    bitArr.SetAll(true);

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

    bitArr.SetAll(false);

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

  }
}

Output

Elements of BitArray after setting true:
        Index 0: True
        Index 1: True
        Index 2: True
        Index 3: True
        Index 4: True
Elements of BitArray  after setting false:
        Index 0: False
        Index 1: False
        Index 2: False
        Index 3: False
        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.