Csharp/C Sharp by API/System.Collections/Stack
Stack.Count
using System;
using System.Collections;
public class Starter {
public static void Main() {
Stack numbers = new Stack(new int[] { 1, 2, 3, 4, 5, 6 });
int total = numbers.Count;
for (int count = 0; count < total; ++count) {
Console.WriteLine(numbers.Pop());
}
}
}
Stack.Peek()
using System;
using System.Collections;
public class TesterStackDemo{
public void Run(){
Stack intStack = new Stack();
for (int i = 0;i<8;i++){
intStack.Push(i*5);
}
Console.Write( "intStack values:\t" );
DisplayValues( intStack );
Console.WriteLine( "\n(Pop)\t{0}",intStack.Pop() );
Console.Write( "intStack values:\t" );
DisplayValues( intStack );
Console.WriteLine( "\n(Pop)\t{0}",intStack.Pop() );
Console.Write( "intStack values:\t" );
DisplayValues( intStack );
Console.WriteLine( "\n(Peek) \t{0}",intStack.Peek() );
Console.Write( "intStack values:\t" );
DisplayValues( intStack );
}
public static void DisplayValues(IEnumerable myCollection ){
foreach (object o in myCollection){
Console.WriteLine(o);
}
}
static void Main()
{
TesterStackDemo t = new TesterStackDemo();
t.Run();
}
}
Stack.ToArray()
using System;
using System.Collections;
public class TesterStackArray
{
public void Run()
{
Stack intStack = new Stack();
for (int i = 1;i<5;i++){
intStack.Push(i*5);
}
const int arraySize = 10;
int[] testArray = new int[arraySize];
for (int i = 1; i < arraySize; i++)
{
testArray[i] = i * 100;
}
Console.WriteLine("\nContents of the test array");
DisplayValues( testArray );
intStack.CopyTo( testArray, 3 );
Console.WriteLine( "\nTestArray after copy: ");
DisplayValues( testArray );
Object[] myArray = intStack.ToArray();
Console.WriteLine( "\nThe new array:" );
DisplayValues( myArray );
}
public static void DisplayValues(IEnumerable myCollection ){
foreach (object o in myCollection){
Console.WriteLine(o);
}
}
static void Main()
{
TesterStackArray t = new TesterStackArray();
t.Run();
}
}