Collections in C#

Microsoft has introduced a concept called collections that consists of set of classes that provides array functionality in a superior manner.

There are the following 2 types of collections:

  1. Non-Generic
  2. Generic

 Non-generic                          Generic

  ArrayList     ------------->          List
  HashTable  ------------->          Dictionary
  SortedList   ------------->          SortedList  
  Stack           ------------->          Stack
  Queue         ------------->          Queue

1. Non-Generic
  1. Each element can represent a value of a different type.
  2. Array Size is not fixed.
  3. Elements can be added / removed at runtime.

ArrayList

  1. Arraylist is a class that is similar to an array, but it can be used to store values of various types.
  2. An Arraylist doesn't have a specific size.
  3. Any number of elements can be stored.
  1. using System.Collections;

  2. protected void Button1_Click(object sender, EventArgs e)  
  3. {  
  4.      ArrayList al = new ArrayList();  
  5.      string str = "kiran teja jallepalli";  
  6.      int x = 7;  
  7.      DateTime d = DateTime.Parse("8-oct-1985");  
  8.      al.Add(str);  
  9.      al.Add(x);  
  10.      al.Add(d);  
  11.       
  12.      foreach (object o in al)  
  13.      {  
  14.         Response.Write(o);  
  15.         Response.Write("<br>");  
  16.      }   
  17. }

    Output:

    kiran teja jallepalli
    7
    10/8/1985 12:00:00 AM
ForeachLoop
It executes for each and every item that exists in the arraylist object.
Every time the loop rotates it reads one item from the arraylist and assignes it to the variable.
Note:
Arraylist allocates memory for 4 items, whenever an object is created. When a fifth item is added, memory for another 4 items are added.
it reduces the memory allocated for the object.
 
Capacity: is a property that returns the number of items for which memory is allocated .
 
HashTable

HashTable is similar to arraylist but represents the items as a combination of a key and value.
  1. using System.Collections;

  2. protected void Button2_Click(object sender, EventArgs e)  
  3. {  
  4.     Hashtable ht = new Hashtable();  
  5.     ht.Add("ora""oracle");  
  6.     ht.Add("vb""vb.net");  
  7.     ht.Add("cs""cs.net");  
  8.     ht.Add("asp""asp.net");  
  9.   
  10.     foreach (DictionaryEntry d in ht)  
  11.     {  
  12.        Response.Write (d.Key + " " + d.Value);  
  13.   
  14.        Response.Write("<br>");  
  15.   
  16.     }  
  17. }

    Output:

    vb  vb.net
    asp asp.net
    cs  cs.net
    ora oracle
DictonaryEntry: is a class whose object represents the data in a combination of key & value pairs.
SortedList: 
    1. Is a class that has the combination of arraylist and hashtable.
    2. Represents the data as a key and value pair.
    3. Arranges all the items in sorted order. 
    1. using System.Collections;

    2. protected void Button3_Click(object sender, EventArgs e)  
    3. {  
    4.      SortedList sl = new SortedList();  
    5.      sl.Add("ora""oracle");  
    6.      sl.Add("vb""vb.net");  
    7.      sl.Add("cs""cs.net");  
    8.      sl.Add("asp""asp.net");  
    9.              
    10.      foreach (DictionaryEntry d in sl)  
    11.      {  
    12.          Response.Write(d.Key + " " + d.Value);  
    13.          Response.Write("<br>");  
    14.   
    15.      }  
    16. }

      Output:

      asp  asp.net
      cs   cs.net
      ora  oracle
      vb   vb.net
    Stack:
    1. protected void Button4_Click(object sender, EventArgs e)  
    2. {  
    3.     Stack stk = new Stack();  
    4.     stk.Push("cs.net");  
    5.     stk.Push("vb.net");  
    6.     stk.Push("asp.net");  
    7.     stk.Push("sqlserver");  
    8.        
    9.     foreach (object o in stk)  
    10.     {  
    11.        Response.Write(o + "<br>");  
    12.     }  
    13. }

      Output:

      sqlserver
      asp.net
      vb.net
      cs.net  
    queue:
    1. using System.Collections;

    2. protected void Button5_Click(object sender, EventArgs e)  
    3. {  
    4.     Queue q = new Queue();  
    5.     q.Enqueue("cs.net");  
    6.     q.Enqueue("vb.net");  
    7.     q.Enqueue("asp.net");  
    8.     q.Enqueue("sqlserver");  
    9.          
    10.     foreach (object o in q)  
    11.     {  
    12.        Response.Write(o + "<br>");  
    13.     }  
    14. }  

      Output:

      cs.net
      vb.net
      asp.net
      sqlserver

    2. Generic Collections

    Generic Collections work on the specific type that is specified in the program whereas non-generic collections work on the object type. 
    1. Specific type
    2. Array Size is not fixed
    3. Elements can be added / removed at runtime.

    List:

    1. using System.Collections.Generic;  
    2.   
    3. protected void Button1_Click(object sender, EventArgs e)  
    4. {  
    5.         List<int> lst = new List<int>();  
    6.         lst.Add(100);  
    7.         lst.Add(200);  
    8.         lst.Add(300);  
    9.         lst.Add(400);  
    10.         foreach (int i in lst)  
    11.         {  
    12.             Response.Write(i+"<br>");  
    13.         }  
    14.  }  
    Dictonary:
    1. using System.Collections.Generic;    
    2.     
    3.  protected void Button1_Click(object sender, EventArgs e)    
    4.  {    
    5.      Dictionary<intstring> dct = new Dictionary<intstring>();    
    6.      dct.Add(1, "cs.net");    
    7.      dct.Add(2, "vb.net");    
    8.      dct.Add(3, "vb.net");    
    9.      dct.Add(4, "vb.net");    
    10.      foreach (KeyValuePair<intstring> kvp in dct)    
    11.      {    
    12.          Response.Write(kvp.Key + " " + kvp.Value);    
    13.          Response.Write("<br>");    
    14.      }          
    15.   }  
    SortedList:
    1. using System.Collections.Generic;

    2. protected void Button3_Click(object sender, EventArgs e)  
    3. {  
    4. SortedList<stringstring> sl = new SortedList<stringstring>();  
    5. sl.Add("ora""oracle");  
    6. sl.Add("vb""vb.net");  
    7. sl.Add("cs""cs.net");  
    8. sl.Add("asp""asp.net");  
    9.   
    10. foreach (KeyValuePair<stringstring> kvp in sl)  
    11. {  
    12. Response.Write(kvp.Key + " " + kvp.Value);  
    13. Response.Write("<br>");  
    14. }  

    Stack:
    1. using System.Collections.Generic; 

    2. protected void Button4_Click(object sender, EventArgs e)  
    3. {  
    4. Stack<string> stk = new Stack<string>();  
    5. stk.Push("cs.net");  
    6. stk.Push("vb.net");  
    7. stk.Push("asp.net");  
    8. stk.Push("sqlserver");  
    9.   
    10. foreach (string s in stk)  
    11. {  
    12. Response.Write(s + "<br>");  
    13. }  

    Queue:
    1. using System.Collections.Generic;
    2. protected void Button1_Click(object sender, EventArgs e)  
    3.    {  
    4.    Queue<string> q = new Queue<string>();  
    5.   
    6.    q.Enqueue("cs.net");  
    7.    q.Enqueue("vb.net");  
    8.    q.Enqueue("asp.net");  
    9.    q.Enqueue("sqlserver");  
    10.   
    11.    foreach (string s in q)  
    12.    {  
    13.    Response.Write(s + "<br>");  
    14.    }  
    15.    } 

    Up Next
      Ebook Download
      View all
      Learn
      View all