Mega Code Archive

 
Categories / C# / Collections Data Structure
 

SortedList Add Method Adds an element with key and value to a SortedList object

using System; using System.Collections; public class SamplesSortedList  {    public static void Main()  {       // Creates and initializes a new SortedList.       SortedList mySL = new SortedList();       mySL.Add( "one", "The" );       mySL.Add( "two", "quick" );       mySL.Add( "three", "brown" );       mySL.Add( "four", "fox" );       Console.WriteLine( "The SortedList contains the following:" );       PrintKeysAndValues( mySL );    }    public static void PrintKeysAndValues( SortedList myList )  {       Console.WriteLine( "\t-KEY-\t-VALUE-" );       for ( int i = 0; i < myList.Count; i++ )  {          Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );       }       Console.WriteLine();    } }