目录

SortedList

SortedList类表示按键排序的键 - 值对的集合,可通过键和索引访问。

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

SortedList类的方法和属性

下表列出了SortedList类的一些常用properties -

Sr.No. 财产和描述
1

Capacity

获取或设置SortedList的容量。

2

Count

获取SortedList中包含的元素数。

3

IsFixedSize

获取一个值,该值指示SortedList是否具有固定大小。

4

IsReadOnly

获取一个值,该值指示SortedList是否为只读。

5

Item

获取并设置与SortedList中的特定键关联的值。

6

Keys

获取SortedList中的键。

7

Values

获取SortedList中的值。

下表列出了SortedList类的一些常用methods -

Sr.No. 方法和描述
1

public virtual void Add(object key, object value);

将具有指定键和值的元素添加到SortedList中。

2

public virtual void Clear();

从SortedList中删除所有元素。

3

public virtual bool ContainsKey(object key);

确定SortedList是否包含特定键。

4

public virtual bool ContainsValue(object value);

确定SortedList是否包含特定值。

5

public virtual object GetByIndex(int index);

获取SortedList的指定索引处的值。

6

public virtual object GetKey(int index);

获取SortedList的指定索引处的键。

7

public virtual IList GetKeyList();

获取SortedList中的键。

8

public virtual IList GetValueList();

获取SortedList中的值。

9

public virtual int IndexOfKey(object key);

返回SortedList中指定键的从零开始的索引。

10

public virtual int IndexOfValue(object value);

返回SortedList中第一次出现的指定值的从零开始的索引。

11

public virtual void Remove(object key);

从SortedList中删除具有指定键的元素。

12

public virtual void RemoveAt(int index);

删除SortedList的指定索引处的元素。

13

public virtual void TrimToSize();

将容量设置为SortedList中的实际元素数。

例子 (Example)

以下示例演示了该概念 -

using System;
using System.Collections;
namespace CollectionsApplication {
   class Program {
      static void Main(string[] args) {
         SortedList sl = new SortedList();
         sl.Add("001", "Zara Ali");
         sl.Add("002", "Abida Rehman");
         sl.Add("003", "Joe Holzner");
         sl.Add("004", "Mausam Benazir Nur");
         sl.Add("005", "M. Amlan");
         sl.Add("006", "M. Arif");
         sl.Add("007", "Ritesh Saikia");
         if (sl.ContainsValue("Nuha Ali")) {
            Console.WriteLine("This student name is already in the list");
         } else {
            sl.Add("008", "Nuha Ali");
         }
         // get a collection of the keys. 
         ICollection key = sl.Keys;
         foreach (string k in key) {
            Console.WriteLine(k + ": " + sl[k]);
         }
      }
   }
}

编译并执行上述代码时,会产生以下结果 -

001: Zara Ali
002: Abida Rehman
003: Joe Holzner
004: Mausam Banazir Nur
005: M. Amlan 
006: M. Arif
007: Ritesh Saikia
008: Nuha Ali
↑回到顶部↑
WIKI教程 @2018