本文整理汇总了C#中System.Collections.SortedList类的典型用法代码示例。如果您正苦于以下问题:C# SortedList类的具体用法?C# SortedList怎么用?C# SortedList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SortedList类属于System.Collections命名空间,在下文中一共展示了SortedList类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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");
// 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();
}
}
输出:
mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: !
示例2: foreach
foreach (DictionaryEntry de in mySortedList)
{
//...
}