Share via


BitArray.CopyTo Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub CopyTo ( _
    array As Array, _
    index As Integer _
)
public void CopyTo(
    Array array,
    int index
)

Parameters

  • array
    Type: System.Array
    The one-dimensional Array that is the destination of the elements copied from BitArray. The Array must have zero-based indexing.
  • index
    Type: System.Int32
    The zero-based index in array at which copying begins.

Implements

ICollection.CopyTo(Array, Int32)

Exceptions

Exception Condition
ArgumentNullException

array is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is less than zero.

ArgumentException

array is multidimensional.

-or-

The number of elements in the source BitArray is greater than the available space from index to the end of the destination array.

InvalidCastException

The type of the source BitArray cannot be cast automatically to the type of the destination array.

Remarks

The specified array must be of a compatible type. Only bool, int, and byte types of arrays are supported.

This method uses Array.Copy to copy the elements.

This method is an O(n) operation, where n is Count.

Examples

The following code example shows how to copy a BitArray into a one-dimensional Array.

Imports System.Collections

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Creates and initializes the source BitArray.
      Dim myBA As New BitArray(4)
      myBA(0) = True
      myBA(1) = True
      myBA(2) = True
      myBA(3) = True

      ' Creates and initializes the one-dimensional target Array of type Boolean.
      Dim myBoolArray(7) As Boolean
      myBoolArray(0) = False
      myBoolArray(1) = False

      ' Displays the values of the target Array.
      outputBlock.Text &= "The target Boolean Array contains the following (before and after copying):" & vbCrLf
      PrintValues(outputBlock, myBoolArray)

      ' Copies the entire source BitArray to the target BitArray, starting at index 3.
      myBA.CopyTo(myBoolArray, 3)

      ' Displays the values of the target Array.
      PrintValues(outputBlock, myBoolArray)

      ' Creates and initializes the one-dimensional target Array of type integer.
      Dim myIntArray(7) As Integer
      myIntArray(0) = 42
      myIntArray(1) = 43

      ' Displays the values of the target Array.
      outputBlock.Text &= "The target Boolean Array contains the following (before and after copying):" & vbCrLf
      PrintValues(outputBlock, myIntArray)

      ' Copies the entire source BitArray to the target BitArray, starting at index 3.
      myBA.CopyTo(myIntArray, 3)

      ' Displays the values of the target Array.
      PrintValues(outputBlock, myIntArray)

      ' Creates and initializes the one-dimensional target Array of type integer.
      Dim myByteArray As Array = Array.CreateInstance(GetType(Byte), 8)
      myByteArray.SetValue(System.Convert.ToByte(10), 0)
      myByteArray.SetValue(System.Convert.ToByte(11), 1)

      ' Displays the values of the target Array.
      outputBlock.Text &= "The target Boolean Array contains the following (before and after copying):" & vbCrLf
      PrintValues(outputBlock, myByteArray)

      ' Copies the entire source BitArray to the target BitArray, starting at index 3.
      myBA.CopyTo(myByteArray, 3)

      ' Displays the values of the target Array.
      PrintValues(outputBlock, myByteArray)

      ' Returns an exception if the array is not of type Boolean, integer or byte.
      Try
         Dim myStringArray As Array = Array.CreateInstance(GetType(String), 8)
         myStringArray.SetValue("Hello", 0)
         myStringArray.SetValue("World", 1)
         myBA.CopyTo(myStringArray, 3)
      Catch myException As Exception
         outputBlock.Text &= "Exception: " + myException.ToString() & vbCrLf
      End Try

   End Sub 'Main

   Public Shared Sub PrintValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArr As IEnumerable)
      Dim obj As [Object]
      For Each obj In myArr
         outputBlock.Text += String.Format("{0,8}", obj)
      Next obj
      outputBlock.Text &= vbCrLf
   End Sub 'PrintValues

End Class 'SamplesBitArray 


' This code produces the following output.
' 
' The target Boolean Array contains the following (before and after copying):
'    False   False   False   False   False   False   False   False
'    False   False   False    True    True    True    True   False
' The target Boolean Array contains the following (before and after copying):
'       42      43       0       0       0       0       0       0
'       42      43       0      15       0       0       0       0
' The target Boolean Array contains the following (before and after copying):
'       10      11       0       0       0       0       0       0
'       10      11       0      15       0       0       0       0
' Exception: System.ArgumentException: Only supported array types for CopyTo on BitArrays are Boolean[], Int32[] and Byte[].
'    at System.Collections.BitArray.CopyTo(Array array, Int32 index)
'    at SamplesBitArray.Main()

using System;
using System.Collections;
public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Creates and initializes the source BitArray.
      BitArray myBA = new BitArray(4);
      myBA[0] = myBA[1] = myBA[2] = myBA[3] = true;

      // Creates and initializes the one-dimensional target Array of type Boolean.
      bool[] myBoolArray = new bool[8];
      myBoolArray[0] = false;
      myBoolArray[1] = false;

      // Displays the values of the target Array.
      outputBlock.Text += "The target Boolean Array contains the following (before and after copying):" + "\n";
      PrintValues(outputBlock, myBoolArray);

      // Copies the entire source BitArray to the target BitArray, starting at index 3.
      myBA.CopyTo(myBoolArray, 3);

      // Displays the values of the target Array.
      PrintValues(outputBlock, myBoolArray);

      // Creates and initializes the one-dimensional target Array of type integer.
      int[] myIntArray = new int[8];
      myIntArray[0] = 42;
      myIntArray[1] = 43;

      // Displays the values of the target Array.
      outputBlock.Text += "The target Boolean Array contains the following (before and after copying):" + "\n";
      PrintValues(outputBlock, myIntArray);

      // Copies the entire source BitArray to the target BitArray, starting at index 3.
      myBA.CopyTo(myIntArray, 3);

      // Displays the values of the target Array.
      PrintValues(outputBlock, myIntArray);

      // Creates and initializes the one-dimensional target Array of type integer.
      Array myByteArray = Array.CreateInstance(typeof(byte), 8);
      myByteArray.SetValue((byte)10, 0);
      myByteArray.SetValue((byte)11, 1);

      // Displays the values of the target Array.
      outputBlock.Text += "The target Boolean Array contains the following (before and after copying):" + "\n";
      PrintValues(outputBlock, myByteArray);

      // Copies the entire source BitArray to the target BitArray, starting at index 3.
      myBA.CopyTo(myByteArray, 3);

      // Displays the values of the target Array.
      PrintValues(outputBlock, myByteArray);

      // Returns an exception if the array is not of type Boolean, integer or byte.
      try
      {
         Array myStringArray = Array.CreateInstance(typeof(String), 8);
         myStringArray.SetValue("Hello", 0);
         myStringArray.SetValue("World", 1);
         myBA.CopyTo(myStringArray, 3);
      }
      catch (Exception myException)
      {
         outputBlock.Text += "Exception: " + myException.ToString() + "\n";
      }
   }

   public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, IEnumerable myArr)
   {
      foreach (Object obj in myArr)
      {
         outputBlock.Text += String.Format("{0,8}", obj);
      }
      outputBlock.Text += "\n";
   }

}


/* 
This code produces the following output.

The target Boolean Array contains the following (before and after copying):
   False   False   False   False   False   False   False   False
   False   False   False    True    True    True    True   False
The target Boolean Array contains the following (before and after copying):
      42      43       0       0       0       0       0       0
      42      43       0      15       0       0       0       0
The target Boolean Array contains the following (before and after copying):
      10      11       0       0       0       0       0       0
      10      11       0      15       0       0       0       0
Exception: System.ArgumentException: Only supported array types for CopyTo on BitArrays are Boolean[], Int32[] and Byte[].
   at System.Collections.BitArray.CopyTo(Array array, Int32 index)
   at SamplesBitArray.Main()

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.