Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Create a SortedList using the default comparer

using System; using System.Collections; using System.Globalization; public class SamplesSortedList {     public static void Main()     {         Hashtable myHT = new Hashtable();         myHT.Add("FIRST", "Hello");         myHT.Add("SECOND", "World");         myHT.Add("THIRD", "!");         // Create a SortedList using the default comparer.         SortedList mySL1 = new SortedList(myHT);         Console.WriteLine("mySL1 (default):");         try         {             mySL1.Add("first", "Ola!");         }         catch (ArgumentException e)         {             Console.WriteLine(e);         }         PrintKeysAndValues(mySL1);     }     public static void PrintKeysAndValues(SortedList myList)     {         for (int i = 0; i < myList.Count; i++)         {             Console.WriteLine(myList.GetKey(i));             Console.WriteLine(myList.GetByIndex(i));         }     } }