OrderedDictionary.Item[] 屬性

定義

取得或設定指定的值。

多載

Item[Int32]

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

Item[Object]

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

Item[Int32]

來源:
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# 語言會使用此 關鍵詞來定義索引器,而不是實作 Item[] 屬性。 Visual Basic 會 Item[] 實作為 預設屬性,以提供相同的索引功能。

適用於

Item[Object]

來源:
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 (!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中。

適用於