SortedList 类

定义

表示键/值对的集合,这些键值对按键排序并可按照键和索引访问。

public ref class SortedList : System::Collections::IDictionary
public ref class SortedList : ICloneable, System::Collections::IDictionary
public class SortedList : System.Collections.IDictionary
public class SortedList : ICloneable, System.Collections.IDictionary
[System.Serializable]
public class SortedList : ICloneable, System.Collections.IDictionary
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class SortedList : ICloneable, System.Collections.IDictionary
type SortedList = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
type SortedList = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface ICloneable
[<System.Serializable>]
type SortedList = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SortedList = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SortedList = class
    interface IDictionary
    interface ICloneable
    interface ICollection
    interface IEnumerable
Public Class SortedList
Implements IDictionary
Public Class SortedList
Implements ICloneable, IDictionary
继承
SortedList
属性
实现

示例

下面的代码示例演示如何创建和初始化 SortedList 对象,以及如何输出其键和值。

#using <system.dll>

using namespace System;
using namespace System::Collections;
public ref class SamplesSortedList
{
public:
   static void PrintKeysAndValues( SortedList^ myList )
   {
      Console::WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList->Count; i++ )
      {
         Console::WriteLine( "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex( i ) );

      }
      Console::WriteLine();
   }

};

int main()
{

   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( "Third", "!" );
   mySL->Add( "Second", "World" );
   mySL->Add( "First", "Hello" );

   // Displays the properties and values of the SortedList.
   Console::WriteLine( "mySL" );
   Console::WriteLine( "  Count:    {0}", mySL->Count );
   Console::WriteLine( "  Capacity: {0}", mySL->Capacity );
   Console::WriteLine( "  Keys and Values:" );
   SamplesSortedList::PrintKeysAndValues( mySL );
}

/*
This code produces the following output.

mySL
Count:    3
Capacity: 16
Keys and Values:
-KEY-    -VALUE-
First:    Hello
Second:    World
Third:    !
*/
using System;
using System.Collections;

public class SamplesSortedList2
{
    public static void Main()
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();
        mySL.Add("Third", "!");
        mySL.Add("Second", "World");
        mySL.Add("First", "Hello");

        // Displays the properties and values of the SortedList.
        Console.WriteLine("mySL");
        Console.WriteLine("  Count:    {0}", mySL.Count);
        Console.WriteLine("  Capacity: {0}", mySL.Capacity);
        Console.WriteLine("  Keys and Values:");
        PrintKeysAndValues(mySL);
    }

    public static void PrintKeysAndValues(SortedList myList)
    {
        Console.WriteLine("\t-KEY-\t-VALUE-");
        for (int i = 0; i < myList.Count; i++)
        {
            Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i));
        }
        Console.WriteLine();
    }
}
/*
This code produces the following output.

mySL
  Count:    3
  Capacity: 16
  Keys and Values:
    -KEY-    -VALUE-
    First:    Hello
    Second:    World
    Third:    !
*/
Imports System.Collections

Public Class SamplesSortedList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add("Third", "!")
        mySL.Add("Second", "World")
        mySL.Add("First", "Hello")
        
        ' Displays the properties and values of the SortedList.
        Console.WriteLine("mySL")
        Console.WriteLine("  Count:    {0}", mySL.Count)
        Console.WriteLine("  Capacity: {0}", mySL.Capacity)
        Console.WriteLine("  Keys and Values:")
        PrintKeysAndValues(mySL)
    End Sub
    
    Public Shared Sub PrintKeysAndValues(myList As SortedList)
        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
           "-VALUE-")
        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _
               "{1}", myList.GetKey(i), myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' mySL
'   Count:    3
'   Capacity: 16
'   Keys and Values:
'     -KEY-     -VALUE-
'     First:    Hello
'     Second:   World
'     Third:    !

注解

SortedList元素可以通过键(如任何实现中的元素)或索引(如任何IDictionaryIList实现中的元素)进行访问。

重要

不建议使用 SortedList 类进行新的开发。 建议改用泛型 System.Collections.Generic.SortedList<TKey,TValue> 类。 有关详细信息,请参阅 GitHub 上 不应使用非泛型集合

SortedList对象在内部维护两个数组来存储列表的元素;即,一个数组用于键,另一个数组用于关联值。 每个元素都是可作为 对象访问 DictionaryEntry 的键/值对。 键不能是 null,但值可以是 。

对象的容量 SortedList 是 可以容纳的元素 SortedList 数。 将元素添加到 时 SortedList,容量会根据需要通过重新分配自动增加。 可以通过调用 TrimToSize 或 显式设置 属性来 Capacity 减小容量。

仅.NET Framework:对于非常大SortedList的对象,可以通过在运行时环境中将配置元素的 <gcAllowVeryLargeObjects> 属性设置为 enabled ,将 64 位系统上的最大容量增加到 true 20 亿个元素。

对象的元素SortedList根据创建 时SortedList指定的特定IComparer实现或IComparable键本身提供的实现按键排序。 在任一 SortedList 情况下, 都不允许重复键。

索引序列基于排序序列。 添加元素时,它将按正确的排序顺序插入到 中 SortedList ,索引会相应地调整。 删除元素时,索引也会相应地进行调整。 因此,在对象中添加或移除元素时,特定键/值对的 SortedList 索引可能会更改。

由于排序, SortedList 对对象的操作往往比对 Hashtable 对象的操作慢。 但是, SortedList 允许通过关联的键或索引访问值,从而提供了更大的灵活性。

可以使用整数索引访问此集合中的元素。 此集合中的索引从零开始。

foreach Visual Basic) for each 中 C# 语言 (语句返回集合中元素类型的对象。 由于对象的每个元素 SortedList 都是键/值对,因此元素类型不是键的类型或值的类型。 相反,元素类型为 DictionaryEntry。 例如:

for each (DictionaryEntry de in mySortedList)
{
    //...
}
foreach (DictionaryEntry de in mySortedList)
{
    //...
}
For Each de As DictionaryEntry In mySortedList
    '...
Next de

语句 foreach 是枚举器的包装器,它只允许从集合读取,而不允许写入集合。

构造函数

SortedList()

初始化 SortedList 类的新实例,该实例为空、具有默认初始容量并根据 IComparable 接口(此接口由添加到 SortedList 对象中的每个键实现)进行排序。

SortedList(IComparer)

初始化 SortedList 类的新实例,该实例为空、具有默认初始容量并根据指定的 IComparer 接口进行排序。

SortedList(IComparer, Int32)

初始化 SortedList 类的新实例,该实例为空、具有指定的初始容量并根据指定的 IComparer 接口排序。

SortedList(IDictionary)

初始化 SortedList 类的新实例,该实例包含从指定字典复制的元素、具有与所复制的元素数相同的初始容量并根据由每个键实现的 IComparable 接口排序。

SortedList(IDictionary, IComparer)

初始化 SortedList 类的新实例,该实例包含从指定字典复制的元素、具有与所复制的元素数相同的初始容量并根据指定的 IComparer 接口排序。

SortedList(Int32)

初始化 SortedList 类的新实例,该实例为空、具有指定的初始容量并且根据 IComparable 接口(此接口由添加到 SortedList 对象的每个键实现)进行排序。

属性

Capacity

获取或设置 SortedList 对象的容量。

Count

获取 SortedList 对象中包含的元素数。

IsFixedSize

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

IsReadOnly

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

IsSynchronized

获取一个值,该值指示对 SortedList 对象的访问是否同步(线程安全)。

Item[Object]

获取或设置与 SortedList 对象中的特定键相关联的值。

Keys

获取 SortedList 对象中的键。

SyncRoot

获取一个对象,该对象可用于同步对 SortedList 对象的访问。

Values

获取 SortedList 对象中的值。

方法

Add(Object, Object)

将带有指定键和值的元素添加到 SortedList 对象。

Clear()

SortedList 对象中移除所有元素。

Clone()

创建 SortedList 对象的浅表副本。

Contains(Object)

确定 SortedList 对象是否包含特定键。

ContainsKey(Object)

确定 SortedList 对象是否包含特定键。

ContainsValue(Object)

确定 SortedList 对象是否包含特定值。

CopyTo(Array, Int32)

从指定数组索引开始将 SortedList 元素复制到一维 Array 对象中。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetByIndex(Int32)

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

GetEnumerator()

返回一个循环访问 IDictionaryEnumerator 对象的 SortedList 对象。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetKey(Int32)

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

GetKeyList()

获取 SortedList 对象中的键。

GetType()

获取当前实例的 Type

(继承自 Object)
GetValueList()

获取 SortedList 对象中的值。

IndexOfKey(Object)

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

IndexOfValue(Object)

返回指定的值在 SortedList 对象中第一个匹配项的从零开始的索引。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Remove(Object)

SortedList 对象中移除带有指定键的元素。

RemoveAt(Int32)

移除 SortedList 对象的指定索引处的元素。

SetByIndex(Int32, Object)

替换 SortedList 对象中指定索引处的值。

Synchronized(SortedList)

返回 SortedList 对象的同步(线程安全)包装。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
TrimToSize()

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

显式接口实现

IEnumerable.GetEnumerator()

返回循环访问 IEnumeratorSortedList

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

线程安全性

Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。

SortedList只要集合未修改,对象就可以同时支持多个读取器。 若要保证 的 SortedList线程安全,所有操作都必须通过 方法返回的 Synchronized(SortedList) 包装器来完成。

枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。

另请参阅