8. Collections and Generics

The .NET Framework support for Collections. Collections are enhancement to the arrays and are part of the System.Collections namespace. There are two distinct collection types in C#. The standard collections, which are found under the System.Collections namespace and the generic collections, under System.Collections.Generic.

ArrayList: The ArrayList class is a dynamic array. Itimplements the IList interface. An ArrayList can hold data of multiple data types

namespace: System.Collections namespace.


ArrayList example code
        //Example Listing 8.2
        using System;
        using System.Collections;
        namespace QSSTraining
        {
            /// 
            /// ArrayList
            /// 
            public class Program
            {
                private static void Main(string[] args)
                {
                    double balance = 100.75;
                    string address = "Atlanta, Dunwooody";
                    int postalCode = 30338;
           
                    ArrayList arrayLists = new ArrayList();
                    arrayLists.Add(balance);
                    arrayLists.Add(address);
                    arrayLists.Add(postalCode);
                    foreach (var v in arrayLists)
                        Console.WriteLine(v);
                }
            }
        }
        

Queue

Represents a first-in, first-out collection of objects.


Queue example code
        //Example Listing 8.5
        using System;
        using System.Collections;
        namespace QSSTraining
        {
            /// 
            /// Queue and  Enqueue
            /// 
            public class Program
            {
                private static void Main(string[] args)
                {
                    Queue oQueue = new Queue();
                    oQueue.Enqueue("One");
                    oQueue.Enqueue("Two");
                    oQueue.Enqueue("Three");
                    foreach (var q in oQueue)
                        Console.WriteLine(q.ToString());
                }
            }
        }
        

Stack:

Represents a simple last-in-first-out (LIFO) non-generic collection of objects.


Stack example code
        //Example Listing 8.4
        using System;
        using System.Collections;
        namespace QSSTraining
        {
            /// 
            /// Stack 
            /// 
            public class Program
            {
                private static void Main(string[] args)
                {
                    // Creates and initializes a new Stack.
                    Stack oStack = new Stack();
                    oStack.Push("One");
                    oStack.Push("Two");
                    oStack.Push("Three");
                    foreach (var s in oStack)
                        Console.WriteLine(s.ToString());
                }
            }
        }
        
[back to top]

Hashtable:

Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key


Hashtable example code
        //Example Listing 8.4
        using System;
        using System.Collections;
        namespace QSSTraining
        {
            /// 
            ///Hashtable
            /// 
            public class Program
            {
                private static void Main(string[] args)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("GA", "Georgia");
                    ht.Add("AL", "Alabama");
                    // the elements are retrieved as KeyValuePair objects.
                    Console.WriteLine();
                    foreach (DictionaryEntry entry in ht)
                    {
                        Console.WriteLine("Key = {0}, Value = {1}", entry.Key, entry.Value);
                    }
                }
            }
        }
        

Generric Collections

Generic Collections lets specify Type and that can hold data of same type and can decide what type of data that collections can hold at runtime. Generic collections are type safe, secure and reduce overhead performance without type conversions. . No boxing, casting and unboxing is done in generics which degrades the performance.

The following are Generic Collections. 1. List<T> 2. Dictionary<K, V> 3. Queue<T> 4. Stack<T>

List<T>:

List type provides an efficient and dynamically allocated array and can grow as needed when we keep add the elements. This is similar to ArrayList (non generic collections).


List<int> example code
        //Example Listing 8.4
        using System;
        using System.Collections.Generic;
        namespace QSSTraining
        {
            /// 
            ///List
            /// 
            public class Program
            {
                private static void Main(string[] args)
                {
                    List<int>list = new List<int>();
                    list.Add(1);
                    list.Add(2);
                    list.Add(3);
                    list.Add(4);
                    list.Add(5);
                    for (int i = 0; i < list.Count; i++)
                    {
                        Console.WriteLine(list[i]);
                    }
                }
            }
        }
        

Dictionary<T>:

Dictionary type is efficient way to store key value pair for lookup and represents a collection of keys and values. This is similary to Hashtable (non generic collections).


Dictionary<string,string> example code
        //Example Listing 8.4
        using System;
        using System.Collections;
        using System.Collections.Generic;
        using ConsoleApp;
        namespace QSSTraining
        {
            /// 
            ///Dictionary
            /// 
            public class Program
            {
                private static void Main(string[] args)
                {
                    Dictionary<string, string> states = new Dictionary<string, string>()
	                {
                        {"AL", "Alabama"},
	                    {"GA", "Georgia"}
	                };
                    foreach (KeyValuePair kvp in states)
                    {
                        Console.WriteLine("Key = {0}, Value = {1}",
                            kvp.Key, kvp.Value);
                    }
                }
            }
        }
        

Queue<T>:

Queue<T> is a generic collection that represents a First-In-First-Out (FIFO) collection of objects. This is similar to non generic collections Queue.
Example:
Queue<int> objQueue = new Queue<int> ();
Queue<string> objQueue = new Queue<string> ();


Queue<string> example code
        //Example Listing 8.6
        using System;
        using System.Collections.Generic;
        namespace QSSTraining
        {
          public class Program
            {
                private static void Main(string[] args)
                {
                    Queue<string> oQueue = new Queue<string>();
                    for (int i = 1; i <= 5; i++)
                    {
                        Console.WriteLine("Enqueue: " + i);
                        oQueue.Enqueue(i.ToString());
                    }
                    for (int i = 1; i <= 5; i++)
                    {
                        Console.WriteLine("Peek: " + oQueue.Peek());
                        Console.WriteLine("Dequeue: " + oQueue.Dequeue());
                    }
                }
            }
        }
        

Stack<T>:

Stack<T> is a generic collection that represents a First-In-First-Out (FIFO) collection of objects. This is similar to non generic collections Stack.
Example:
Stack<int> objStack = new Stack<int> ();
Stack<string> objStack = new Stack<string> ();


Stack<int> example code
        //Example Listing 8.5
        using System;
        using System.Collections.Generic;
        namespace QSSTraining
        {
          public class Program
            {
                private static void Main(string[] args)
                {
                    Stack<int> oStack = new Stack<int>();
                    for (int i = 1; i <= 5; i++)
                    {
                        Console.WriteLine("Push: " + i);
                        oStack.Push(i);
                    }
                    for (int i = 1; i <= 5; i++)
                    {
                        Console.WriteLine("Peek: " + oStack.Peek());
                        Console.WriteLine("Pop: " + oStack.Pop());
                    }
                }
            }
        }