A Stack Component Example


The example in Listing B.3 implements a simple stack component in C#. It demonstrates the use of component-oriented features such as properties, indexers, and XML comments. A more complete example would also implement the ICollection and IList interfaces.

Listing B.3
 using System; /// <summary> /// A general-purpose stack class /// Not thread-safe /// </summary> class Stack {     const int sizeIncrement = 10;     private int count = 0;     private object[] values = new object[sizeIncrement];     /// <summary>     /// The number of elements in the stack     /// </summary>     public virtual int Count     {         get         {             return count;         }     }     /// <summary>     /// Remove the top element from the stack     /// and return it     /// </summary>     /// <returns>The top element</returns>     public virtual object Pop()     {             // Stack is empty         if (count == 0)             throw new InvalidOperationException();         else         {                 // Return top of stack             count;             object ans = values[count];             values[count] = null;             return ans;         }     }     /// <summary>     /// Push an element onto the stack     /// </summary>     /// <param name="o">The object to add</param>     public virtual void Push(object o)     {             // Expand the stack if required         if (count == values.Length)         {             int newSize = values.Length + sizeIncrement;             object[] newValues = new object[newSize];             for (int i = 0; i < values.Length; i++)                 newValues[i] = values[i];             values = newValues;         }         values[count++] = o;     }     /// <summary>     /// Indexer to peek into the current     /// contents of the stack     /// </summary>     public object this[int index]     {         get         {             return(values[count - index - 1]);         }     }     /// <summary>     /// Produce a string representation of the stack     /// </summary>     /// <returns>The string representation</returns>     public override string ToString()     {             // Convert all stack items to strings         string[] args = new string[count];         int index = 0;         for (int i = count - 1; i >= 0; i)         {             args[index] = values[i].ToString();             index++;         }             // and join them together         return String.Join(", ", args);     } } class Test {     public static void Main()     {         Stack s = new Stack();         for (int i = 1; i <= 15; i++)         {             s.Push(i);         }         Console.WriteLine("Stack s = {0}", s);         Console.WriteLine("Stack[0] = {0}", s[0]);         Console.WriteLine("Stack[5] = {0}", s[5]);         while (s.Count > 0)             Console.WriteLine("Popped {0}", s.Pop());         Console.WriteLine("Stack s = {0}", s);     } } 


Programming in the .NET Environment
Programming in the .NET Environment
ISBN: 0201770180
EAN: 2147483647
Year: 2002
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net