Skip to content

Instantly share code, notes, and snippets.

@AndrewBarfield
Created April 30, 2012 09:51
Show Gist options
  • Save AndrewBarfield/2556928 to your computer and use it in GitHub Desktop.
Save AndrewBarfield/2556928 to your computer and use it in GitHub Desktop.
C#: Working with the Stack class
using System;
using System.Collections;
namespace StackExample {
class Program {
static void Main(string[] args) {
// Creates and initializes a new stack
Stack stackColl = new Stack();
// Inserts objects into the stack
stackColl.Push( "1st Item" );
stackColl.Push( "2nd Item" );
stackColl.Push( "3rd Item" );
stackColl.Push( "4th Item" );
stackColl.Push( "5th Item" );
// Displays properties of the Stack
Console.WriteLine( "Count: {0}", stackColl.Count );
Console.WriteLine( "IsSynchronized: {0}", stackColl.IsSynchronized );
Console.WriteLine( "SyncRoot: {0}", stackColl.SyncRoot );
Console.WriteLine();
// Pops and displays values
Console.WriteLine( "Values:" );
int count = stackColl.Count;
for ( int index = 0 ; index < count ; index++ ) {
Console.WriteLine( stackColl.Pop() );
}
Console.WriteLine();
// Allow the user to see the output
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment