C-Sharpcorner - Latest Articles: Collections in C#

来源:互联网 时间:1970-01-01

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

There are 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 different type.
  2. Array Size is not fixed.
  3. Elements can be added / removed at runtime.

ArrayList

  1. Arraylist is a class which is similar to an array, But it can be used to store values of different types.
  2. Arraylist doesn't has any specific size.
  3. Any number of elements can be stored. 
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.      ArrayList al = new ArrayList();  
  4.      string str = "kiran teja jallepalli";  
  5.      int x = 7;  
  6.      DateTime d = DateTime.Parse("8-oct-1985");  
  7.      al.Add(str);  
  8.      al.Add(x);  
  9.      al.Add(d);  
  10.       
  11.      foreach (object o in al)  
  12.      {  
  13.         Response.Write(o);  
  14.         Response.Write("<br>");  
  15.      }   
  16. }  

HashTable:

HashTable is similar to arraylist but represents the list of items in the combination of key and value pair.

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

  • Is a class which has the combination of arraylist and hashtable.
  • It represents the data in key and value pair.
  • It arranges all the items in the sorted order. 
  1. protected void Button3_Click(object sender, EventArgs e)  
  2. {  
  3.      SortedList sl = new SortedList();  
  4.      sl.Add("ora", "oracle");  
  5.      sl.Add("vb", "vb.net");  
  6.      sl.Add("cs", "cs.net");  
  7.      sl.Add("asp", "asp.net");  
  8.              
  9.      foreach (DictionaryEntry d in sl)  
  10.      {  
  11.          Response.Write(d.Key + " " + d.Value);  
  12.          Response.Write("<br>");  
  13.   
  14.      }  
  15. }  

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. }  

queue:

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

2. Generic Collections

Generic Collections work on the specific type which is mentioned in the program where as in non-generic collections work on 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<int, string> dct = new Dictionary<int, string>();  
  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<int, string> kvp in dct)  
  11.      {  
  12.          Response.Write(kvp.Key + " " + kvp.Value);  
  13.          Response.Write("<br>");  
  14.      }        
  15.   }      

相关阅读:
Top