Collections Provided by the Base Class LibrariesOut 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 ArrayListLike 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 Exampleusing 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 StackA 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 Exampleusing 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 HashtableA 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 Exampleusing 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 BitArrayA BitArray is a collection of bits (true/false) values. Listing 5.8 demonstrates how to create, modify, and iterate through a BitArray.
Listing 5.8. A BitArray Exampleusing 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 QueueA 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 Exampleusing 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 SortedListA 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 Exampleusing 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) { ![]() |