Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Enumerator that iterates through the BitArray in C#
The BitArray class in C# implements the IEnumerable interface, which allows you to iterate through its elements using an enumerator. This enables the use of foreach loops to traverse through each bit value in the array sequentially.
The enumerator returns each bit as a bool value, making it easy to process the individual bits in a BitArray collection.
Syntax
Following is the syntax for iterating through a BitArray using an enumerator −
BitArray bitArray = new BitArray(size);
IEnumerable enumerable = bitArray;
foreach (Object bit in enumerable) {
// Process each bit
}
You can also iterate directly without explicit casting −
foreach (bool bit in bitArray) {
// Process each bit
}
Using Enumerator with BitArray
The following example demonstrates how to iterate through a BitArray using an enumerator −
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr1 = new BitArray(5);
BitArray arr2 = new BitArray(5);
arr1[0] = false;
arr1[1] = true;
arr1[2] = false;
arr1[3] = true;
arr1[4] = true;
Console.WriteLine("Enumerator that iterates through BitArray1");
IEnumerable demoEnum = arr1;
foreach (Object ob in demoEnum) {
Console.WriteLine(ob);
}
arr2[0] = true;
arr2[1] = true;
arr2[2] = true;
arr2[3] = false;
arr2[4] = true;
Console.WriteLine("Enumerator that iterates through BitArray2");
demoEnum = arr2;
foreach (Object ob in demoEnum) {
Console.WriteLine(ob);
}
Console.WriteLine("Is BitArray1 equal to BitArray2? = " + arr1.Equals(arr2));
}
}
The output of the above code is −
Enumerator that iterates through BitArray1 False True False True True Enumerator that iterates through BitArray2 True True True False True Is BitArray1 equal to BitArray2? = False
Direct Iteration with Foreach
You can also iterate through a BitArray directly without explicitly casting to IEnumerable −
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr = new BitArray(5);
arr[0] = false;
arr[1] = true;
arr[2] = false;
arr[3] = true;
arr[4] = true;
Console.WriteLine("Direct iteration through BitArray");
foreach (bool bit in arr) {
Console.WriteLine("Bit value: " + bit);
}
Console.WriteLine("\nCounting true bits:");
int trueCount = 0;
foreach (bool bit in arr) {
if (bit) trueCount++;
}
Console.WriteLine("Number of true bits: " + trueCount);
}
}
The output of the above code is −
Direct iteration through BitArray Bit value: False Bit value: True Bit value: False Bit value: True Bit value: True Counting true bits: Number of true bits: 3
How It Works
The BitArray class implements IEnumerable, which provides the GetEnumerator() method. This method returns an enumerator that can traverse through each bit in the array sequentially. The enumerator maintains the current position and provides access to the current element during iteration.
Conclusion
The enumerator for BitArray in C# provides a convenient way to iterate through bit values using foreach loops. It implements the IEnumerable interface, allowing sequential access to each boolean value in the array, making it easy to process or analyze individual bits in the collection.
