Collections Provided by the Base Class Libraries


Out of the box, C# contains several useful collections. Each of them is slightly different from the other and has its own advantages and disadvantages. This next section gives small examples of how to use each one.

Using an ArrayList

Like an array, an ArrayList is a collection that can be accessed through an index. Listing 5.5 demonstrates how to create, add items to, remove, and iterate through an ArrayList.

Listing 5.5. An ArrayList Example
 using System; using System.Collections; namespace ArrayListCollection {         class Class1         {     static protected ArrayList list = new ArrayList();     protected static void PrintList(System.Collections.ArrayList list)     {       IEnumerator enumerator = list.GetEnumerator();       while(enumerator.MoveNext())       {         System.Console.WriteLine((string) enumerator.Current);       }     }     [STAThread]     static void Main(string[] args)     {       // Add items to list       for(int i=0; i<10; i++)       {         list.Add("Item " + i.ToString());       }       // Iterate through the list.       PrintList(list);       System.Console.WriteLine("*************");       // Remove an Item       list.Remove("Item 7");       PrintList(list);       System.Console.ReadLine();     }   } } 

Using a Stack

A Stack is a simple LIFO (Last In First Out) collection. It introduces three new methods: Push, Pop, and Peek. Listing 5.6 demonstrates how to create, add items to, remove, and iterate through a Stack.

Listing 5.6. A Stack Example
 using System; using System.Collections; namespace StackExample {   class Class1   {     static protected Stack list = new Stack();     protected static void PrintList(System.Collections.Stack list)     {       IEnumerator enumerator = list.GetEnumerator();       while(enumerator.MoveNext())       {         System.Console.WriteLine((string) enumerator.Current);       }     }     [STAThread]     static void Main(string[] args)     {       // Add items to list       for(int i=0; i<10; i++)       {         list.Push("Item " + i.ToString());       }       // Iterate through the list.       PrintList(list);       System.Console.WriteLine("*************");       // Remove an Item       list.Pop();       PrintList(list);       System.Console.ReadLine();     }   } } 

Using a Hashtable

A Hashtable is a dictionary collection. In other words, it is a collection of key/value pairs. To enumerate through this list, you should use IDictionaryEnumerator. The following example, Listing 5.7, demonstrates how to create, add items to, remove, and iterate through a Hashtable.

Listing 5.7. A Hashtable Example
 using System; using System.Collections; namespace HashtableExample {   class Class1   {     static protected Hashtable list = new Hashtable();     protected static void PrintList(System.Collections.Hashtable list)     {       IDictionaryEnumerator enumerator = list.GetEnumerator();       while(enumerator.MoveNext())       {         System.Console.WriteLine((string) enumerator.Value);       }     }     [STAThread]     static void Main(string[] args)     {       // Add items to list       for(int i=0; i<10; i++)       {         list.Add("Item " + i.ToString(), "Item " + i.ToString());       }       // Iterate through the list.       PrintList(list);       System.Console.WriteLine("*************");       // Print a item from the index       System.Console.WriteLine((string) list["Item 7"]);       System.Console.ReadLine();     }   } } 

Using a BitArray

A BitArray is a collection of bits (true/false) values. Listing 5.8 demonstrates how to create, modify, and iterate through a BitArray.

BITMASKING AND BitArrays

A BitArray is, as mentioned, an array of true/false values. There are many cases in software development today when you need to store a list of true/false values that might or might not be of finite length.

In the past, programmers have solved this problem by using integers to do the work of a BitArray. For example, a 32-bit Integer can be used to store 32 individual Boolean values. It creates a very memory- and storage-efficient way of managing lists of Booleans. Getting and setting individual bit values was done with bitwise operators. If you ever find yourself in a situation where you think you need to use bitmasking and bitwise operators for a list of Booleans, consider using the BitArray instead.


Listing 5.8. A BitArray Example
 using System; using System.Collections; namespace BitArrayExample {   class Class1   {     static protected BitArray list = new BitArray(12);     protected static void PrintList(System.Collections.BitArray list)     {       IEnumerator enumerator = list.GetEnumerator();       while(enumerator.MoveNext())       {         System.Console.WriteLine((bool) enumerator.Current);       }     }     [STAThread]     static void Main(string[] args)     {       // Add items to list       for(int i=0; i<10; i++)       {         list.Set(i, (i/2==0));       }       // Iterate through the list.       PrintList(list);       System.Console.ReadLine();     }   } } 

Using the Queue

A Queue is a simple FIFO (First In First Out) collection. The following example, Listing 5.9, demonstrates how to create, add items to, remove, and iterate through a Queue.

Listing 5.9. A Queue Example
 using System; using System.Collections; namespace QueueExample {   class Class1   {     static protected Queue list = new Queue();     protected static void PrintList(System.Collections.Queue list)     {       IEnumerator enumerator = list.GetEnumerator();       while(enumerator.MoveNext())       {         System.Console.WriteLine((string) enumerator.Current);       }     }     [STAThread]     static void Main(string[] args)     {       // Add items to list       for(int i=0; i<10; i++)       {         list.Enqueue("Item " + i.ToString());       }       // Iterate through the list.       PrintList(list);       System.Console.WriteLine("*************");       // Remove an Item       list.Dequeue();       PrintList(list);       System.Console.ReadLine();     }   } } 

Using a SortedList

A SortedList is a hybrid collection. It is a mixture of an ArrayList and a Hashtable. The following example, Listing 5.10, demonstrates how to create, add items to, remove, and iterate through a SortedList .

Listing 5.10. A SortedList Example
    using System;    using System.Collections;    namespace SortedListExample    {      class Class1      {        static protected SortedList list = new SortedList();        protected static void PrintList(System.Collections.SortedList list)        {          IDictionaryEnumerator enumerator = list.GetEnumerator();          while(enumerator.MoveNext())          {            System.Console.WriteLine((string) enumerator.Value);          }        }        [STAThread]        static void Main(string[] args)        {          // Add items to list          for(int i=0; i<10; i++)          {            list.Add("Item " + i.ToString(), "Item " + i.ToString());          }          // Iterate through the list.          PrintList(list);          System.Console.WriteLine("*************");          // Print a item from the index (Access Like a Hashtable)          System.Console.WriteLine((string) list["Item 7"]);          // Print a item from the index (Access Like a Arraylist)          System.Console.WriteLine((string) list.GetByIndex(7));          System.Console.ReadLine();        }      }    } 



    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

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