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元素可以透過其索引鍵來存取,例如任何 IDictionary 實作中的專案,或透過其索引來存取,例如任何 IList 實作中的專案。

重要

不建議您將 類別用於 SortedList 新的開發。 相反地,建議您使用泛型 System.Collections.Generic.SortedList<TKey,TValue> 類別。 如需詳細資訊,請參閱不應在 GitHub 上使用 非泛型集合

物件會在內部維護兩個 SortedList 陣列來儲存清單的專案;也就是說,索引鍵的一個陣列,另一個陣列用於相關聯的值。 每個元素都是可做為 DictionaryEntry 物件的索引鍵/值組。 索引鍵不能是 null ,但值可以是 。

物件的容量 SortedList 是 可以保存的專案 SortedList 數目。 當元素新增至 SortedList 時,容量會自動透過重新配置而增加。 您可以藉由呼叫 TrimToSize 或 明確設定 Capacity 屬性來減少容量。

僅.NET Framework:對於非常大 SortedList 的物件,您可以將 64 位系統上的最大容量增加到 200 億個元素,方法是在執行時間環境中將組態元素的 <gcAllowVeryLargeObjects> 屬性設定 enabledtrue

物件的元素 SortedList 會根據建立 時 SortedList 指定的特定實作,或根據 IComparable 索引鍵本身所提供的實作,依索引鍵本身所指定的特定 IComparer 實作排序。 不論是哪一 SortedList 種情況,都不允許重複的索引鍵。

索引序列是以排序次序為基礎。 新增專案時,它會以正確的排序次序插入 , SortedList 而且索引編制會據以調整。 移除專案時,索引也會據以調整。 因此,當加入或移除物件時,特定索引鍵/值組的 SortedList 索引可能會變更。

物件上的 SortedList 作業通常會比物件上的 Hashtable 作業慢,因為排序。 不過,藉 SortedList 由允許透過相關聯的索引鍵或索引存取值,提供更大的彈性。

這個集合中的元素可以使用整數索引來存取。 這個集合中的索引是以零起始。

foreachVisual Basic 中 C# 語言 (的 語句) for each 會傳回集合中專案類型的 物件。 因為物件的每個元素 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 物件的淺層複本 (Shallow Copy)。

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()

傳回透過 IEnumerator 重複的 SortedList

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

執行緒安全性

Visual Basic 中的公用靜態 (Shared) 此類型的成員是安全線程。 並非所有的執行個體成員都是安全執行緒。

只要未修改集合,物件 SortedList 就可以同時支援多個讀取器。 若要保證 的 SortedList 執行緒安全性,所有作業都必須透過 方法傳 Synchronized(SortedList) 回的包裝函式來完成。

透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱