C# Stack CopyTo
Description
Stack CopyTo
copies the Stack to an existing one-dimensional
Array, starting at the specified array index.
Syntax
Stack.CopyTo
has the following syntax.
public virtual void CopyTo(
Array array,
int index
)
Parameters
Stack.CopyTo
has the following 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.
Returns
Stack.CopyTo
method returns
Example
The following example shows how to copy a Stack into a one-dimensional array.
// w w w. j av a 2s. c o m
using System;
using System.Collections;
public class SamplesStack {
public static void Main() {
Stack mySourceQ = new Stack();
mySourceQ.Push( "A" );
mySourceQ.Push( "B" );
mySourceQ.Push( "C" );
Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
mySourceQ.CopyTo( myTargetArray, 2 );
foreach ( Object myObj in myTargetArray ) {
Console.WriteLine(myObj );
}
}
}
The code above generates the following result.