Last Updated : 11 Jul, 2025
The
BitArrayclass manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is
oni.e,
1and false indicates the bit is
offi.e,
0. This class is contained in
System.Collectionsnamespace.
BitArray.CopyTo(Array, Int32)method is used to copy the entire BitArray
to a compatible one-dimensional Array,starting at the specified index of the target array.
Properties:public void CopyTo (Array arr, int index);Parameters:
Below given are some examples to understand the implementation in a better way:
Example 1: CSHARP
// C# code to copy BitArray to Array,
// starting at the specified index
// of the target array
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a BitArray
BitArray myBitArr = new BitArray(4);
myBitArr[0] = true;
myBitArr[1] = true;
myBitArr[2] = true;
myBitArr[3] = true;
// Creating a bool array
bool[] myBoolArr = new bool[8];
myBoolArr[0] = false;
myBoolArr[1] = false;
// Copying BitArray to Array,
// starting at the specified index
// of the target array
myBitArr.CopyTo(myBoolArr, 3);
// Displaying elements in myBoolArr
foreach(Object obj in myBoolArr)
{
Console.WriteLine(obj);
}
}
}
Output:
False False False True True True True FalseExample 2: CSHARP
// C# code to copy BitArray to Array,
// starting at the specified index
// of the target array
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a BitArray
BitArray myBitArr = new BitArray(3);
myBitArr[0] = true;
myBitArr[1] = true;
myBitArr[2] = true;
// Creating a bool array
bool[] myBoolArr = new bool[8];
myBoolArr[0] = false;
myBoolArr[1] = false;
myBoolArr[2] = false;
// Copying BitArray to Array,
// starting at the specified index
// of the target array
// This should raise "ArgumentOutOfRangeException"
// as index is less than 0
myBitArr.CopyTo(myBoolArr, -2);
// Displaying elements in myBoolArr
foreach(Object obj in myBoolArr)
{
Console.WriteLine(obj);
}
}
}
Runtime Error:
Unhandled Exception: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: indexNote:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4