C# 中 SortedList 类的 Keys 属性是什么?

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

使用 C# 中 SortedList 类的 keys 属性获取 SortedList 中的键。我们首先使用元素设置了 SortedList 属性。

SortedList sl = new SortedList();
sl.Add("ST0","One");
sl.Add("ST1","Two");
sl.Add("ST2","Three");
sl.Add("ST3","Four");
sl.Add("ST4","Five");
sl.Add("ST5","Six");
sl.Add("ST6", "Seven");

现在使用 keys 属性获取键。

ICollection key = sl.Keys;

以下是实现 SortedList 类的 keys 属性的完整代码。

示例

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");
         sl.Add("ST3", "Four");
         sl.Add("ST4", "Five");
         sl.Add("ST5", "Six");
         sl.Add("ST6", "Seven");
         ICollection key = sl.Keys;
         foreach (string k in key) {
            Console.WriteLine(k);
         }
      }
   }
}

输出

ST0
ST1
ST2
ST3
ST4
ST5
ST6

相关文章