在 C# 中獲取 SortedList 物件指定索引處的 value


要獲取 SortedList 物件指定索引處的 value,程式碼如下 −

範例

 實際演示

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

更新於: 06-Dec-2019

64 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告