Enumerator that iterates through the BitArray in C#

CsharpServer Side ProgrammingProgramming

Following is the code that iterates through the BitArray with Enumerator −

Example

 Live Demo

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));
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example −

 Live Demo

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("Enumerator that iterates through BitArray");
      IEnumerable demoEnum = arr;
      foreach(Object ob in demoEnum){
         Console.WriteLine(ob);
      }
   }
}

Output

This will produce the following output −

Enumerator that iterates through BitArray
False
True
False
True
True
raja
Published on 10-Dec-2019 16:36:54
Advertisements