OrderedDictionary 類別

定義

表示可依索引鍵或索引存取的索引鍵/值組集合。

C#
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary
C#
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
C#
[System.Serializable]
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
C#
public class OrderedDictionary : System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
繼承
OrderedDictionary
衍生
屬性
實作

範例

下列程式代碼範例示範集合的OrderedDictionary建立、母體擴展和修改,以及兩種顯示 內容的OrderedDictionary技巧:一種是使用 和 Values 屬性,另一種是Keys透過 GetEnumerator 方法建立列舉值。

C#
// The following code example enumerates the elements of a OrderedDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;

public class OrderedDictionarySample
{
    public static void Main()
    {

        // Creates and initializes a OrderedDictionary.
        OrderedDictionary myOrderedDictionary = new OrderedDictionary();
        myOrderedDictionary.Add("testKey1", "testValue1");
        myOrderedDictionary.Add("testKey2", "testValue2");
        myOrderedDictionary.Add("keyToDelete", "valueToDelete");
        myOrderedDictionary.Add("testKey3", "testValue3");

        ICollection keyCollection = myOrderedDictionary.Keys;
        ICollection valueCollection = myOrderedDictionary.Values;

        // Display the contents using the key and value collections
        DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);

        // Modifying the OrderedDictionary
        if (!myOrderedDictionary.IsReadOnly)
        {
            // Insert a new key to the beginning of the OrderedDictionary
            myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");

            // Modify the value of the entry with the key "testKey2"
            myOrderedDictionary["testKey2"] = "modifiedValue";

            // Remove the last entry from the OrderedDictionary: "testKey3"
            myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);

            // Remove the "keyToDelete" entry, if it exists
            if (myOrderedDictionary.Contains("keyToDelete"))
            {
                myOrderedDictionary.Remove("keyToDelete");
            }
        }

        Console.WriteLine(
            "{0}Displaying the entries of a modified OrderedDictionary.",
            Environment.NewLine);
        DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);

        // Clear the OrderedDictionary and add new values
        myOrderedDictionary.Clear();
        myOrderedDictionary.Add("newKey1", "newValue1");
        myOrderedDictionary.Add("newKey2", "newValue2");
        myOrderedDictionary.Add("newKey3", "newValue3");

        // Display the contents of the "new" Dictionary using an enumerator
        IDictionaryEnumerator myEnumerator =
            myOrderedDictionary.GetEnumerator();

        Console.WriteLine(
            "{0}Displaying the entries of a \"new\" OrderedDictionary.",
            Environment.NewLine);

        DisplayEnumerator(myEnumerator);
    }

    // Displays the contents of the OrderedDictionary from its keys and values
    public static void DisplayContents(
        ICollection keyCollection, ICollection valueCollection, int dictionarySize)
    {
        String[] myKeys = new String[dictionarySize];
        String[] myValues = new String[dictionarySize];
        keyCollection.CopyTo(myKeys, 0);
        valueCollection.CopyTo(myValues, 0);

        // Displays the contents of the OrderedDictionary
        Console.WriteLine("   INDEX KEY                       VALUE");
        for (int i = 0; i < dictionarySize; i++)
        {
            Console.WriteLine("   {0,-5} {1,-25} {2}",
                i, myKeys[i], myValues[i]);
        }
        Console.WriteLine();
    }

    // Displays the contents of the OrderedDictionary using its enumerator
    public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
    {
        Console.WriteLine("   KEY                       VALUE");
        while (myEnumerator.MoveNext())
        {
            Console.WriteLine("   {0,-25} {1}",
                myEnumerator.Key, myEnumerator.Value);
        }
    }
}

/*
This code produces the following output.

   INDEX KEY                       VALUE
   0     testKey1                  testValue1
   1     testKey2                  testValue2
   2     keyToDelete               valueToDelete
   3     testKey3                  testValue3


Displaying the entries of a modified OrderedDictionary.
   INDEX KEY                       VALUE
   0     insertedKey1              insertedValue1
   1     testKey1                  testValue1
   2     testKey2                  modifiedValue


Displaying the entries of a "new" OrderedDictionary.
   KEY                       VALUE
   newKey1                   newValue1
   newKey2                   newValue2
   newKey3                   newValue3

*/

備註

每個元素都是儲存在物件中的 DictionaryEntry 索引鍵/值組。 索引鍵不能是 null,但值可以是 。

OrderedDictionary 元素不會依索引鍵排序,與類別的 SortedDictionary<TKey,TValue> 元素不同。 您可以透過索引鍵或索引來存取元素。

foreach Visual Basic 中 C# 語言 (For Each 的語句) 會傳回集合中每個元素類型的 物件。 因為集合的每個元素 OrderedDictionary 都是索引鍵/值組,所以元素類型不是索引鍵的類型或值的型別。 相反地,元素類型為 DictionaryEntry。 下列程式代碼顯示 C#、Visual Basic 和 C++ 語法。

C#
foreach (DictionaryEntry de in myOrderedDictionary)
{
    //...
}

語句 foreach 是列舉值周圍的包裝函式,它只允許讀取集合,而不寫入集合。

建構函式

OrderedDictionary()

初始化 OrderedDictionary 類別的新執行個體。

OrderedDictionary(IEqualityComparer)

使用指定之比較子來初始化 OrderedDictionary 類別的新執行個體。

OrderedDictionary(Int32)

使用指定之初始容量來初始化 OrderedDictionary 類別的新執行個體。

OrderedDictionary(Int32, IEqualityComparer)

使用指定的初始容量和比較子,初始化 OrderedDictionary 類別的新執行個體。

OrderedDictionary(SerializationInfo, StreamingContext)
已淘汰.

使用指定的 OrderedDictionarySerializationInfo 物件,初始化 StreamingContext 類別的執行個體,這個執行個體是可序列化的。

屬性

Count

取得包含在 OrderedDictionary 集合中的索引鍵/值組數目。

IsReadOnly

取得值,表示 OrderedDictionary 集合是否為唯讀。

Item[Int32]

取得或設定指定之索引處的值。

Item[Object]

取得或設定具有指定之索引鍵的值。

Keys

取得 ICollection 物件,其中包含 OrderedDictionary 集合中的索引鍵。

Values

取得 ICollection 物件,其中包含 OrderedDictionary 集合中的值。

方法

Add(Object, Object)

將具有指定之索引鍵和值的元素加入至含有最低可用索引的 OrderedDictionary 集合中。

AsReadOnly()

傳回目前 OrderedDictionary 集合的唯讀複本。

Clear()

OrderedDictionary 集合移除所有元素。

Contains(Object)

判斷 OrderedDictionary 集合是否包含特定索引鍵。

CopyTo(Array, Int32)

OrderedDictionary 元素複製到指定之索引處的一維 Array 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回會逐一查看 IDictionaryEnumerator 集合的 OrderedDictionary 物件。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

實作 ISerializable 介面,並傳回序列化 OrderedDictionary 集合所需的資料。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Insert(Int32, Object, Object)

使用指定的索引鍵和值,將新元素插入 OrderedDictionary 集合中指定的索引處。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnDeserialization(Object)

實作 ISerializable 介面並在還原序列化完成時,由還原序列化事件回呼。

Remove(Object)

OrderedDictionary 集合移除具有指定之索引鍵的元素。

RemoveAt(Int32)

OrderedDictionary 集合移除指定之索引處的元素。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

取得值,指出對 OrderedDictionary 物件的存取是否已同步處理 (安全執行緒)。

ICollection.SyncRoot

取得可用來同步處理對 OrderedDictionary 物件之存取的物件。

IDeserializationCallback.OnDeserialization(Object)

實作 ISerializable 介面並在還原序列化完成時,由還原序列化事件回呼。

IDictionary.IsFixedSize

取得值,指出 OrderedDictionary 是否有固定的大小。

IEnumerable.GetEnumerator()

傳回會逐一查看 IDictionaryEnumerator 集合的 OrderedDictionary 物件。

擴充方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0