Home »
.Net »
C# Programs
C# - BitArray.CopyTo() Method with Example
In this tutorial, we will learn about the C# BitArray.CopyTo() method with its definition, usage, syntax, and example.
By Nidhi Last updated : March 31, 2023
BitArray.CopyTo() Method
The BitArray.CopyTo() method is used to copy BitArray elements to a one-dimensional array of Boolean values, starting at the specified index of an array.
Syntax
void BitArray.CopyTo(Array array, int arrayIndex);
Parameter(s)
- array: An array to copy elements of BitArray.
- index: The index in array at which copying begins.
Return Value
It does not return any value.
Exception(s)
- System.ArgumentNullException
- System.ArgumentOutOfRangeException
- System.ArgumentException
- System.InvalidCastException
C# Example of BitArray.CopyTo() Method
The source code to copy the entire BitArray to a compatible one-dimensional Array 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 bitArr1 = new BitArray(5);
bool[] Arr = new bool[8];
int index = 0;
bitArr1[0] = true;
bitArr1[1] = false;
bitArr1[2] = true;
bitArr1[3] = false;
bitArr1[4] = true;
bitArr1.CopyTo(Arr, 0);
Console.WriteLine("Elements of BitArray1:");
for (index = 0; index < bitArr1.Length; index++) {
Console.WriteLine("\tIndex " + index + ": " + bitArr1.Get(index));
}
Console.WriteLine("Array Values:");
for (index = 0; index < Arr.Length; index++) {
Console.WriteLine("\t" + Arr[index]);
}
}
}
Output
Elements of BitArray1:
Index 0: True
Index 1: False
Index 2: True
Index 3: False
Index 4: True
Array Values:
True
False
True
False
True
False
False
False
Press any key to continue . . .
C# BitArray Class Programs »