C# Stack ToArray

Description

Stack ToArray copies the Stack to a new array.

Syntax

Stack.ToArray has the following syntax.


public virtual Object[] ToArray()

Returns

Stack.ToArray method returns

Example

The following example shows how to copy a Stack into a one-dimensional array.


//from  www  . j a v a  2 s. 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.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack