排序列表用SortedList对象表示,用Add()方法会自动将元素插入到适当的位置以保持关键字的顺序
/* Example11_8.cs illustrates the use of a SortedList */ using System; using System.Collections; class Example11_8 { public static void Main() { // create a SortedList object SortedList mySortedList = new SortedList(); // add elements containing US state abbreviations and state // names to mySortedList using the Add() method mySortedList.Add("NY", "New York"); mySortedList.Add("FL", "Florida"); mySortedList.Add("AL", "Alabama"); mySortedList.Add("WY", "Wyoming"); mySortedList.Add("CA", "California"); // get the state name value for "CA" string myState = (string) mySortedList["CA"]; Console.WriteLine("myState = " + myState); // get the state name value at index 3 using the GetByIndex() method string anotherState = (string) mySortedList.GetByIndex(3); Console.WriteLine("anotherState = " + anotherState); // display the keys for mySortedList using the Keys property foreach (string myKey in mySortedList.Keys) { Console.WriteLine("myKey = " + myKey); } // display the values for mySortedList using the Values property foreach(string myValue in mySortedList.Values) { Console.WriteLine("myValue = " + myValue); } } }