微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 中的 SortedList 类是什么?

C# 中的 SortedList 类是什么?

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Let us see an example wherein we added 4 key and value pair for SortedList −

Example

using System;
using System.Collections;

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

         s.Add("S1", "Maths");
         s.Add("S2", "Science");
         s.Add("S3", "English");
         s.Add("S4", "Economics");
      }
   }
}

让我们现在来看看如何获取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");
         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);
         }
      }
   }
}

以上就是C# 中的 SortedList 类是什么?的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐