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-Jun-2020

160 次瀏覽

開啟你的 職業生涯

獲得課程認證

開始