什麼是 C# 中基於鍵的 I/O 集合?


C# 中基於鍵的 I/O 集合就是我們所說的 SortedList −

SortedList<TKey,TValue>

SortedList 類表示一個關鍵值對的集合,這些關鍵值對按鍵排序,並且可以透過鍵和索引進行訪問。以下是如何將它們新增到 SortedList 中 −

s.Add("Sub1", "Physics");
s.Add("Sub2", "Chemistry");
s.Add("Sub3", "Biology");
s.Add("Sub4", "Java");

以下是顯示 SortedList 中鍵和值的示例 −

示例

 現場演示

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         SortedList s = new SortedList();

         s.Add("Sub1", "Economics");
         s.Add("Sub2", "Accountancy");
         s.Add("Sub3", "Business Studies");
         s.Add("Sub4", "English");

         Console.WriteLine("Capacity = " + s.Capacity);

         // get a collection of the keys.
         ICollection key = s.Keys;

         foreach (string k in key) {
            Console.WriteLine(k + ": " + s[k]);
         }
      }
   }
}

輸出

Capacity = 16
Sub1: Economics
Sub2: Accountancy
Sub3: Business Studies
Sub4: English

更新日期:20-6 月-2020 年

160 次瀏覽

開啟你的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.