C# Stack ToArray

Description

Copies the Stack to a new array.

Syntax

Stac.ToArray has the following syntax.


public T[] ToArray()

Example

Copies the Stack to a new array.


using System;//from   w w  w  . j av a2  s. c  o  m
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Stack<string> numbers = new Stack<string>();
        numbers.Push("one");
        numbers.Push("two");
        numbers.Push("three");
        numbers.Push("four");
        numbers.Push("five");

        Stack<string> stack2 = new Stack<string>(numbers.ToArray());
        foreach( string number in stack2 )
        {
            Console.WriteLine(number);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack