Team LiB
Previous Section Next Section

Stacks

A Stack represents a simple last-in, first-out (LIFO) collection of type. A Stack is implemented as a circular buffer, where the last item in is always the first item out.

Caution 

Like a Queue, a Stack is not thread-safe, and it requires the Synchronize method to prevent simultaneous access.

To create a new Stack, you must declare and pass it the number of items that you expect to add to the Stack, like this:

            Stack a = new Stack(10);
            int x = 0;

To add items, call the Push method, passing it the item to be added to the Stack as the first parameter. You can add items, as long as the property count does not exceed the maximum size of the Stack when it was declared. Here is an example of adding items to the Stack:

               a.Push(x);
               x++;
               a.Push(x);

To iterate through a Stack, you can use a foreach loop. The first operand is the same type that you are pushing onto the Stack, and the second operand is the Stack object itself, as shown in this example:

               foreach(int y in a)
               {
                     Console.WriteLine(y);
               }

To remove items from the top of the Stack, call the Pop method.

               a.Pop();

You can also call the Clear method to remove all items from the Stack.

               a.Clear();

The following example demonstrates using Stacks.

Code Example: Stacks
Start example

using System;
using System.Collections;

namespace Client.Chapter_3___Structs__Enums__Arrays_and_Collections
{
      class Stacks
      {
            static void Main(string[] args)
            {
                  //Creates a stack with 10 members
                  Stack a = new Stack(10);
                  int x = 0;
                  //Adds members to the stack
                  a.Push(x);
                  x++;
                  a.Push(x);
                  foreach (int y in a)
                  {
                        Console.WriteLine(y);
                  }
                  //Removes members from a stack
                  a.Pop();
                  //Removes all members of the stack
                  a.Clear();
            }
      }
}
End example

Choosing the Right Collection

Do you need temporary storage?

  • If yes, use a Queue or Stack.

  • If no, consider another collection.

Do you need to access elements of the collection in a specific order, LIFO or FIFO?

  • A Queue provides an FIFO order.

  • A Stack provides an LIFO order.

The other collections provide random access.

Do you need to access elements by an index?

  • ArrayList and StringCollection provide zero-based index access.

  • Hashtable, SortedList, ListDictionary, and StringDictionary provide access via keys.

  • NamedObjectCollectionBase and NameValueCollectionBase provide access by both zero-based index and by key/value pairs.

Will elements store a single value or a key/value pair?

  • For one value, use any collection based on IList.

  • For a key/single-value pair, use any IDictionary-derived class.

  • For a key/multivalued pair, use or derive from NameValueCollection or a class in Collections.Specialized.

Do you need to sort elements differently from how they were entered into the array?

  • Hashtable sorts by the hash code of the key.

  • SortedList sorts by the value of key.

  • ArrayList provides a Sort method that takes an IComparer-derived class.

Do you need fast searches?

  • ListDictionary is best for small collections of ten or fewer items.

  • Use a Hashtable.

Do you need a collection that accepts only strings?

  • Use StringCollection, StringDictionary, or Collections.Specialized.


Team LiB
Previous Section Next Section