語言

OrderedDictionary.Item[] 屬性

定義

取得或設定指定的值。

多載

名稱 Description
Item[Int32]

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

Item[Object]

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

Item[Int32]

來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs

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

public:
 property System::Object ^ default[int] { System::Object ^ get(int index); void set(int index, System::Object ^ value); };
public object this[int index] { get; set; }
public object? this[int index] { get; set; }
member this.Item(int) : obj with get, set
Default Public Property Item(index As Integer) As Object

參數

index
Int32

這是要取得或設定的值的零基指數。

屬性值

項目在指定索引下的價值。

實作

例外狀況

屬性正在設定中,集合 OrderedDictionary 是唯讀的。

index 小於零。

-或-

index 等於或大於 Count。

備註

此特性允許您透過以下語法存取集合中的特定元素: myCollection[index]。

C# 語言使用 this 關鍵字來定義索引器,而非實作該 Item[] 屬性。 Visual Basic 將 Item[] 實作為 default 屬性,提供相同的索引功能。

適用於

Item[Object]

來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs
來源:
OrderedDictionary.cs

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

public:
 property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
public object this[object key] { get; set; }
public object? this[object key] { get; set; }
member this.Item(obj) : obj with get, set
Default Public Property Item(key As Object) As Object

參數

key
Object

關鍵在於要取得或設定的價值。

屬性值

與指定鍵相關聯的值。 若找不到指定的鍵,嘗試取得該鍵時會回傳 null,並嘗試設定該鍵,並建立一個新元素。

實作

例外狀況

屬性正在設定中,集合 OrderedDictionary 是唯讀的。

範例

以下程式碼範例示範了集合的 OrderedDictionary 修改。 在此範例中, Item[] 屬性用來修改字典條目,鍵為 "testKey2"。 此程式碼是更大程式碼範例的一部分,可於 OrderedDictionary。

// 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");
    }
}
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then

    ' 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")) Then
        myOrderedDictionary.Remove("keyToDelete")
    End If
End If

備註

此特性允許您透過以下語法存取集合中的特定元素: myCollection[key]。

你也可以利用這個 Item[] 特性來新增元素,方法是設定集合中不存在 OrderedDictionary 的鍵值(例如, myCollection["myNonexistentKey"] = myValue)。 然而,如果指定的鍵已經存在 OrderedDictionary於 中,設定屬性 Item[] 會覆蓋舊值。 相較之下,該 Add 方法不會修改現有元素。

索引鍵不能 null,但值可以是 。 為了區分 是因為找不到指定金鑰而回傳,而 null 是因為指定金鑰的值為 null,請null使用 該Contains方法判斷該金鑰是否存在於 OrderedDictionary中。

適用於