为了获得在SortedList对象的指定索引处的值,代码如下-
using System; using System.Collections; public class Demo { public static void Main(String[] args) { SortedList list = new SortedList(); list.Add("A", "Jacob"); list.Add("B", "Sam"); list.Add("C", "Tom"); list.Add("D", "John"); list.Add("E", "Tim"); list.Add("F", "Mark"); list.Add("G", "Gary"); Console.WriteLine("索引 2 = "+list.GetByIndex(2)); Console.WriteLine("索引 5 = "+list.GetByIndex(5)); Console.WriteLine("索引 6 = "+list.GetByIndex(6)); } }
输出结果
这将产生以下输出-
索引 2 = Tom 索引 5 = Mark 索引 6 = Gary
让我们看另一个例子-
using System; using System.Collections; public class Demo { public static void Main(String[] args) { SortedList list = new SortedList(); list.Add(1, "One"); list.Add(2, "Two"); list.Add(3, "Three"); list.Add(4, "Four"); list.Add(5, "Five"); Console.WriteLine("索引 0 = "+list.GetByIndex(0)); } }
输出结果
这将产生以下输出-
索引 0 = One