From: honda@luckstar.com.tw Date: 2017-01-29 Subject: Collections.SortedList.txt ref: https://msdn.microsoft.com/en-us/library/system.collections.sortedlist(v=vs.110).aspx SortedList Class 1. SortedList有Collection跟Generic兩種版本. 若要存放的元素相同, 則應優先使用Generic版本. 2. 可以Key或Index存取資料. Index的值會跟隨著key的順序變動. 3. 元素以DictionaryEnry型態存放. 4. Key不可為null. 不可重複. 5. Value可以為null. 6. 因為需要維持排序, 因此效能比Hashtable差. 但是彈性較佳(因為可以以Key或Index存取資料) 7. Key跟Value都是Object型態. ---------- SortedList Class 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(); } } /* This code produces the following output. mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! */