Created
April 30, 2012 09:51
-
-
Save AndrewBarfield/2556928 to your computer and use it in GitHub Desktop.
C#: Working with the Stack class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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