C# 中的 SortedMap 接口

csharpprogrammingserver side programming更新于 2025/9/23 18:22:17

Java 中有 SortedMap 接口,而 C# 中与之对应的接口是 SortedList。

C# 中的 SortedList 集合使用键和索引来访问列表中的项目。

排序列表是数组和哈希表的组合。它包含一个可以使用键或索引访问的项目列表。如果使用索引访问项目,则它是一个 ArrayList;如果使用键访问项目,则它是一个 Hashtable。项目集合始终按键值排序。

让我们看一个使用 SortedList 并显示键的示例 −

示例

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         SortedList sl = new SortedList();
         sl.Add("ST0", "One");
         sl.Add("ST1", "Two");
         sl.Add("ST2", "Three");
         ICollection key = sl.Keys;

         foreach(string k in key) {
            Console.WriteLine(k);
         }
      }
   }
}

输出

ST0
ST1
ST2

相关文章