Copying BitArray elements to an Array in C#

CsharpServer Side ProgrammingProgramming

To copy BitArray elements to an Array, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      BitArray arr = new BitArray(2);
      arr[0] = false;
      arr[1] = true;
      Console.WriteLine("Elements in BitArray...");
      foreach (bool res in arr){
         Console.WriteLine(res);
      }
      bool[] boolArr = new bool[2];
      boolArr[0] = true;
      boolArr[1] = false;
      arr.CopyTo(boolArr, 0);
      Console.WriteLine("\nArray...");
      foreach(Object obj in boolArr){
         Console.WriteLine(obj);
      }
   }
}

Output

This will produce the following output −

Elements in BitArray...
False
True
Array...
False
True

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("Elements in BitArray...");
      foreach (bool res in arr){
         Console.WriteLine(res);
      }
      bool[] boolArr = new bool[10];
      boolArr[0] = true;
      boolArr[1] = false;
      arr.CopyTo(boolArr, 0);
      Console.WriteLine("\nArray...");
      foreach(Object obj in boolArr){
         Console.WriteLine(obj);
      }
   }
}

Output

This will produce the following output −

Elements in BitArray...
False
True
False
True
True

Array...
False
True
False
True
True
False
False
False
False
False
raja
Published on 10-Dec-2019 15:07:29
Advertisements