Arrays provide a mechanism for storing a set of data, but it is quite cumbersome to dynamically alter the size of an array. Collection classes provide convenient dynamic containers for storing information and allow dynamic insertion and deletion capabilities. Collection classes are grouped under the System.Collections and System.Collections.Specialized namespaces. Collections implement an IEnumerable interface and provide an Enumerator to iterate over the elements contained in the collection. Key collection classes provided by the .NET Framework include the following:
QueuesIn this section, you'll take a look at how to use a Queue collection. You first write a couple of items into the queue, then browse it, and then empty it (Listing 4.1). Queue provides two basic methods ”Enqueue, which stores an item into the queue, and Dequeue, which removes an item from the queue. As noted earlier, Queue is a first-in first-out (FIFO) collection. Listing 4.1 Creating FIFO Queuesusing System; using System.Threading; using System.Collections; public class QueueExample { public static void Main() { Queue jobs = new Queue(); jobs.Enqueue("Power-on the Computer"); jobs.Enqueue("Login"); jobs.Enqueue("Backup"); jobs.Enqueue("Shutdown"); Console.WriteLine("Jobs to be done:"+jobs.Count); foreach (String job in jobs) { Console.WriteLine(job); } Console.WriteLine("Jobs to be done:"+jobs.Count); int queueCount = jobs.Count; for (int i=0;i<queueCount;i++) { Console.WriteLine(i+":"+jobs.Dequeue()); } Console.WriteLine("Jobs to be done:"+jobs.Count); } } Running the Queue example should yield the following result: Jobs to be done:4 Power-on the Computer Login Backup Shutdown Jobs to be done:4 0:Power-on the Computer 1:Login 2:Backup 3:Shutdown Jobs to be done:0 StacksNext, take a look at how the current program changes when you use a Stack. Similar to the previous example, you first write a couple of items into the Stack, then browse it, and then empty it (Listing 4.2). Stack provides two basic methods ”Push, which stores an item into the stack, and Pop, which removes an item from the stack. As noted earlier, Stack is a last-in first-out (LIFO) collection. Listing 4.2 Creating LIFO Stacksusing System; using System.Threading; using System.Collections; public class StackExample { public static void Main() { Stack jobs = new Stack(); jobs.Push("Power-on the Computer"); jobs.Push("Login"); jobs.Push("Backup"); jobs.Push("Shutdown"); Console.WriteLine("Jobs to be done:"+jobs.Count); foreach (String job in jobs) { Console.WriteLine(job); } Console.WriteLine("Jobs to be done:"+jobs.Count); int StackCount = jobs.Count; for (int i=0;i<StackCount;i++) { Console.WriteLine(i+":"+jobs.Pop()); } Console.WriteLine("Jobs to be done:"+jobs.Count); } } Running the Stack example should yield the following output: Jobs to be done:4 Shutdown Backup Login Power-on the Computer Jobs to be done:4 0:Shutdown 1:Backup 2:Login 3:Power-on the Computer Jobs to be done:0 As you can see, the Stack really doesn't help in implementing job schedules because it reverses the schedule of jobs to be done. Stacks are typically used for expression evaluators . Using HashtablesHashtables provide a convenient key/value pair of storing information accessible through a key. Listing 4.3 shows an example of how to use the Hashtable collection. Listing 4.3 Using Hashtablesusing System; using System.Collections; class HashtableExample { public static void Main() { Hashtable ht = new Hashtable(); ht.Add("DatabaseType","SQL Server"); ht.Add("DatabaseVersion","2000"); ht.Add("ServerName","localhost"); ht.Add("UserId","hks"); ht.Add("Password","mysecret"); foreach (String key in ht.Keys) { Console.WriteLine("{0}={1}",key,ht[key]); } Console.WriteLine("UserId={0}",ht["UserId"]); } } Running the Hashtable example should yield the following output: UserId=hks Password=mysecret DatabaseVersion=2000 DatabaseType=SQL Server ServerName=localhost |