C# Collections Part 3: Sortedlist

Sortedlist:

The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Collections;  
  7.   
  8. namespace sortedList  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             SortedList sl = new SortedList();  
  15.   
  16.             sl.Add(1,"Shimul Hossain");  
  17.             sl.Add(3,"Sujan Hasan");  
  18.             sl.Add(2,"Shakil Sorker");  
  19.             print(sl);  
  20.             sl.Remove(2);  
  21.             print(sl);  
  22.             Console.ReadKey();  
  23.         }  
  24.   
  25.         public static void print(SortedList slist)  
  26.         {  
  27.             foreach (var i in slist.Keys)  
  28.             {  
  29.                 Console.WriteLine(i+" : "+slist[i]);  
  30.             }  
  31.         }  
  32.     }  
  33. }  
Share:

0 Comments:

Post a Comment