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 类是什么?的详细内容,更多请关注编程之家其它相关文章!

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...