Stack(T) Class represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.
using System;
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");
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine(numbers.Pop());
Console.WriteLine(numbers.Peek());
Console.WriteLine( numbers.Pop());
Stack<string> stack2 = new Stack<string>(numbers.ToArray());
foreach( string number in stack2 )
{
Console.WriteLine(number);
}
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
Stack<string> stack3 = new Stack<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in stack3 )
{
Console.WriteLine(number);
}
Console.WriteLine(stack2.Contains("four"));
stack2.Clear();
Console.WriteLine(stack2.Count);
}
}
Related examples in the same category