Export (0) Print
Expand All

Stack.CopyTo Method

Copies the Stack to an existing one-dimensional Array, starting at the specified array index.

[Visual Basic]
Public Overridable Sub CopyTo( _
   ByVal array As Array, _
   ByVal index As Integer _
) Implements ICollection.CopyTo
[C#]
public virtual void CopyTo(
 Array array,
 int index
);
[C++]
public: virtual void CopyTo(
 Array* array,
 int index
);
[JScript]
public function CopyTo(
   array : Array,
 index : int
);

Parameters

array
The one-dimensional Array that is the destination of the elements copied from Stack. The Array must have zero-based indexing.
index
The zero-based index in array at which copying begins.

Implements

ICollection.CopyTo

Exceptions

Exception Type Condition
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 Stack is greater than the available space from index to the end of the destination array.

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

Remarks

The elements are copied onto the array in a last-in-first-out order, similar to the order of the elements returned by a succession of calls to Pop.

Example

[Visual Basic, C#, C++] The following example shows how to copy a Stack into a one-dimensional Array instance and into a one-dimensional standard array.

[Visual Basic] 
Imports System
Imports System.Collections

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes the source Stack.
        Dim mySourceQ As New Stack()
        mySourceQ.Push("barn")
        mySourceQ.Push("the")
        mySourceQ.Push("in")
        mySourceQ.Push("cats")
        mySourceQ.Push("napping")
        mySourceQ.Push("three")
        
        ' Creates and initializes the one-dimensional target Array.
        Dim myTargetArray As Array = Array.CreateInstance(GetType(String), 15)
        myTargetArray.SetValue("The", 0)
        myTargetArray.SetValue("quick", 1)
        myTargetArray.SetValue("brown", 2)
        myTargetArray.SetValue("fox", 3)
        myTargetArray.SetValue("jumped", 4)
        myTargetArray.SetValue("over", 5)
        myTargetArray.SetValue("the", 6)
        myTargetArray.SetValue("lazy", 7)
        myTargetArray.SetValue("dog", 8)
        
        ' Displays the values of the target Array.
        Console.WriteLine("The target Array contains the " & _
           "following (before and after copying):")
        PrintValues(myTargetArray, " "c)
        
        ' Copies the entire source Stack to the target Array, starting
        ' at index 6.
        mySourceQ.CopyTo(myTargetArray, 6)
        
        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)
        
        ' Copies the entire source Stack to a new standard array.
        Dim myStandardArray As Object() = mySourceQ.ToArray()
        
        ' Displays the values of the new standard array.
        Console.WriteLine("The new standard array contains the following:")
        PrintValues(myStandardArray, " "c)
    End Sub    
    
    Overloads Public Shared Sub PrintValues(myArr As Array, _
       mySeparator As Char)
       
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
    
    Overloads Public Shared Sub PrintValues(myArr() As Object, _
       mySeparator As Char)
       
        Dim myObj As Object
        For Each myObj In  myArr
            Console.Write("{0}{1}", mySeparator, myObj)
        Next myObj
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
'
' The target Array contains the following (before and after copying):
'  The quick brown fox jumped over the lazy dog
'  The quick brown fox jumped over three napping cats in the barn
' The new standard array contains the following:
'  three napping cats in the barn 

[C#] 
using System;
using System.Collections;
public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes the source Stack.
      Stack mySourceQ = new Stack();
      mySourceQ.Push( "barn" );
      mySourceQ.Push( "the" );
      mySourceQ.Push( "in" );
      mySourceQ.Push( "cats" );
      mySourceQ.Push( "napping" );
      mySourceQ.Push( "three" );

      // Creates and initializes the one-dimensional target Array.
      Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumped", 4 );
      myTargetArray.SetValue( "over", 5 );
      myTargetArray.SetValue( "the", 6 );
      myTargetArray.SetValue( "lazy", 7 );
      myTargetArray.SetValue( "dog", 8 );

      // Displays the values of the target Array.
      Console.WriteLine( "The target Array contains the following (before and after copying):" );
      PrintValues( myTargetArray, ' ' );

      // Copies the entire source Stack to the target Array, starting at index 6.
      mySourceQ.CopyTo( myTargetArray, 6 );

      // Displays the values of the target Array.
      PrintValues( myTargetArray, ' ' );

      // Copies the entire source Stack to a new standard array.
      Object[] myStandardArray = mySourceQ.ToArray();

      // Displays the values of the new standard array.
      Console.WriteLine( "The new standard array contains the following:" );
      PrintValues( myStandardArray, ' ' );

   }


   public static void PrintValues( Array myArr, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      }
      Console.WriteLine();
   }

   public static void PrintValues( Object[] myArr, char mySeparator )  {
      foreach ( Object myObj in myArr )  {
         Console.Write( "{0}{1}", mySeparator, myObj );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn   
The new standard array contains the following:
 three napping cats in the barn
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


void PrintValues( Array* myArr, Char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myArr->GetEnumerator();
   int i = 0;
   int cols = myArr->GetLength( myArr->Rank - 1 );
   while ( myEnumerator->MoveNext() )  {
      if ( i < cols )  {
         i++;
      } else  {
         Console::WriteLine();
         i = 1;
      }
      Console::Write( S"{0}{1}", __box(mySeparator), myEnumerator->Current );
   }
   Console::WriteLine();
}

void PrintValues( Object* myArr __gc[], Char mySeparator )  {
    for( int i(0); i < myArr->Count; ++i ) {
           Console::Write( S"{0}{1}", __box(mySeparator), myArr[i]->ToString() );
   }
   Console::WriteLine();
}


int main()  {

   // Creates and initializes the source Stack.
   Stack* mySourceQ = new Stack();
   mySourceQ->Push( S"barn" );
   mySourceQ->Push( S"the" );
   mySourceQ->Push( S"in" );
   mySourceQ->Push( S"cats" );
   mySourceQ->Push( S"napping" );
   mySourceQ->Push( S"three" );

   // Creates and initializes the one-dimensional target Array.
   Array* myTargetArray=Array::CreateInstance( __typeof(String), 15 );
   myTargetArray->SetValue( S"The", 0 );
   myTargetArray->SetValue( S"quick", 1 );
   myTargetArray->SetValue( S"brown", 2 );
   myTargetArray->SetValue( S"fox", 3 );
   myTargetArray->SetValue( S"jumped", 4 );
   myTargetArray->SetValue( S"over", 5 );
   myTargetArray->SetValue( S"the", 6 );
   myTargetArray->SetValue( S"lazy", 7 );
   myTargetArray->SetValue( S"dog", 8 );

   // Displays the values of the target Array.
   Console::WriteLine( S"The target Array contains the following (before and after copying):" );
   PrintValues( myTargetArray, ' ' );

   // Copies the entire source Stack to the target Array, starting at index 6.
   mySourceQ->CopyTo( myTargetArray, 6 );

   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );

   // Copies the entire source Stack to a new standard array.
   Object* myStandardArray __gc[] = mySourceQ->ToArray();

   // Displays the values of the new standard array.
   Console::WriteLine( S"The new standard array contains the following:" );
   PrintValues( myStandardArray, ' ' );

}

/*
This code produces the following output.

The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog
 The quick brown fox jumped over three napping cats in the barn
The new standard array contains the following:
 three napping cats in the barn
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Stack Class | Stack Members | System.Collections Namespace | ToArray

Show:
© 2015 Microsoft