C# 中基于键的 I/O 集合是什么?

csharpprogrammingserver side programming更新于 2025/10/14 12:22:17

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

相关文章