Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Create a SortedList using case-insensitive comparer

using System; using System.Collections; using System.Globalization; public class SamplesSortedList {     public static void Main()     {         SortedList mySL2 = new SortedList(new CaseInsensitiveComparer());         Console.WriteLine("mySL2 (case-insensitive comparer):");         mySL2.Add("A", "a");         mySL2.Add("B", "b");         mySL2.Add("C", "c");         try         {             mySL2.Add("first", "aaaaa");         }         catch (ArgumentException e)         {             Console.WriteLine(e);         }         PrintKeysAndValues(mySL2);     }     public static void PrintKeysAndValues(SortedList myList)     {         Console.WriteLine("        -KEY-   -VALUE-");         for (int i = 0; i < myList.Count; i++)         {             Console.WriteLine("        {0,-6}: {1}",                 myList.GetKey(i), myList.GetByIndex(i));         }         Console.WriteLine();     } }