首页 文章资讯内容详情

在C#中获取SortedList对象的指定索引处的值

2026-06-04 1 花语

为了获得在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