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

csharpserver side programmingprogramming

获取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("Value at index 2 = "+list.GetByIndex(2));
      Console.WriteLine("Value at index 5 = "+list.GetByIndex(5));
      Console.WriteLine("Value at index 6 = "+list.GetByIndex(6));
   }
}

输出

这将产生以下输出 −

Value at index 2 = Tom
Value at index 5 = Mark
Value at index 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("Value at index 0 = "+list.GetByIndex(0));
   }
}

输出

这将产生以下输出 −

Value at index 0 = One

相关文章