Export (0) Print
Expand All

BitArray.CopyTo Method

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)

public void CopyTo (
	Array array,
	int index
)
public final void CopyTo (
	Array array, 
	int index
)
public final function CopyTo (
	array : Array, 
	index : int
)
Not applicable.

Parameters

array

The one-dimensional Array that is the destination of the elements copied from BitArray. The Array must have zero-based indexing.

index

The zero-based index in array at which copying begins.

Exception typeCondition

ArgumentNullException

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

ArgumentOutOfRangeException

index is less than zero.

ArgumentException

array is multidimensional.

-or-

index is equal to or greater than the length of array.

-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.

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.

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

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

   public static void Main()  {

      // 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.
      Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" );
      PrintValues( 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( 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.
      Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" );
      PrintValues( 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( 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.
      Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" );
      PrintValues( 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( 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 )  {
         Console.WriteLine("Exception: " + myException.ToString());
      }
   }

   public static void PrintValues( IEnumerable myArr )  {
      foreach ( Object obj in myArr ) {
         Console.Write( "{0,8}", obj );
      }
      Console.WriteLine();
   }

}


/* 
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()

*/

import System.*;
import System.Collections.*;

public class SamplesBitArray
{
    public static void main(String[] args)
    {
        // Creates and initializes the source BitArray.
        BitArray myBA = new BitArray(4);

        myBA.set_Item(0, true);
        myBA.set_Item(1, true);
        myBA.set_Item(2, true);
        myBA.set_Item(3, true);

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

        // Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the following"
            + " (before and after copying):");
        PrintValues(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(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.
        Console.WriteLine("The target Boolean Array contains the following"
            + " (before and after copying):");
        PrintValues(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(myIntArray);

        // Creates and initializes the one-dimensional target Array of 
        // type integer.
        Array myByteArray = Array.CreateInstance(ubyte.class.ToType(), 8);
        myByteArray.SetValue((System.Byte)(10), 0);
        myByteArray.SetValue((System.Byte)(11), 1);

        // Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the following"
            + " (before and after copying):");
        PrintValues(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(myByteArray);

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

    public static void PrintValues(IEnumerable myArr)
    {
        IEnumerator objMyEnum = myArr.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("{0,8}", obj);
        }
        Console.WriteLine();
    } //PrintValues
}//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(String[] args)

 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft