定义
System.Collections.SortedList类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问。
注意:键不能为null,并且不能相同,但值可以。
键相同出错提示如下:其他信息: 已添加项。字典中的关键字:“First”所添加的关键字:“First”
注意:using System.Collections;
例子
using System;
using System.Collections;
public class SamplesSortedList
{
public static void Main()
{
// Creates and initializes a new SortedList.
SortedList mySL = new SortedList();
mySL.Add("Third", "!");
mySL.Add("Second", "World");
mySL.Add("First", "Hello");
mySL.Add("First", "my");
// Displays the properties and values of the SortedList.
Console.WriteLine("mySL");
Console.WriteLine(" Count: {0}", mySL.Count);
Console.WriteLine(" Capacity: {0}", mySL.Capacity);
Console.WriteLine(" Keys and Values:");
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();
}
}
/*
This code produces the following output.
mySL
Count: 3
Capacity: 16
Keys and Values:
-KEY- -VALUE-
First: Hello
Second: World
Third: !
*/
原文:http://fengyuzaitu.blog.51cto.com/5218690/1902907