Use the ContainsKey() method to check if mySortedList contains a key : Generic SortedList « Generic « C# / CSharp Tutorial






using System;
using System.Collections;

class MainClass
{

  public static void Main()
  {
    SortedList mySortedList = new SortedList();

    mySortedList.Add("NY", "New York");
    mySortedList.Add("FL", "Florida");
    mySortedList.Add("AL", "Alabama");
    mySortedList.Add("WY", "Wyoming");
    mySortedList.Add("CA", "California");

    foreach (string myKey in mySortedList.Keys)
    {
      Console.WriteLine("myKey = " + myKey);
    }

    foreach(string myValue in mySortedList.Values)
    {
      Console.WriteLine("myValue = " + myValue);
    }
    
    if (mySortedList.ContainsKey("FL"))
    {
      Console.WriteLine("mySortedList contains the key FL");
    }
  }
}
myKey = AL
myKey = CA
myKey = FL
myKey = NY
myKey = WY
myValue = Alabama
myValue = California
myValue = Florida
myValue = New York
myValue = Wyoming
mySortedList contains the key FL








18.7.Generic SortedList
18.7.1.Use the keys to obtain the values from a generic SortedList
18.7.2.Use the ContainsKey() method to check if mySortedList contains a key