Mega Code Archive

 
Categories / C# / Collections Data Structure
 

SortedList IndexOfKey returns the zero-based index of the specified key in a SortedList object

using System; using System.Collections; public class SamplesSortedList  {    public static void Main()  {       SortedList mySL = new SortedList();       mySL.Add( 1, "one" );       mySL.Add( 3, "three" );       mySL.Add( 2, "two" );       mySL.Add( 4, "four" );       mySL.Add( 0, "zero" );       int myKey = 2;       Console.WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL.IndexOfKey( myKey ) );       String myValue = "three";       Console.WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL.IndexOfValue( myValue ) );    }    public static void PrintIndexAndKeysAndValues( SortedList myList )  {       for ( int i = 0; i < myList.Count; i++ )  {          Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) );       }    } }