OrderedDictionary.Item[] プロパティ

定義

指定した値を取得または設定します。

オーバーロード

Item[Int32]

指定したインデックス位置にある値を取得または設定します。

Item[Object]

指定したキーの値を取得または設定します。

Item[Int32]

指定したインデックス位置にある値を取得または設定します。

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

取得または設定する値の、0 から始まるインデックス番号。

プロパティ値

Object

指定したインデックス位置にある項目の値。

実装

例外

プロパティが設定されていますが、OrderedDictionary コレクションが読み取り専用です。

index が 0 未満です。

または indexCount 以上になっています。

注釈

このプロパティを使用すると、次の構文 myCollection[index]を使用してコレクション内の特定の要素にアクセスできます。

C# 言語では、 この キーワードを使用して、プロパティを実装する代わりにインデクサーを Item[] 定義します。 Visual Basicは、同じインデックス作成機能を提供する既定のプロパティとして実装Item[]されます。

適用対象

Item[Object]

指定したキーの値を取得または設定します。

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

取得または設定する値のキー。

プロパティ値

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[]使用して、コレクションに存在しないキーの値 (たとえば) を設定することで、myCollection["myNonexistentKey"] = myValue新しい要素をOrderedDictionary追加することもできます。 ただし、指定したキーが既に OrderedDictionary存在する場合は、プロパティの設定によって Item[] 古い値が上書きされます。 これに対し、メソッドは既存の Add 要素を変更しません。

キーを指定 nullすることはできませんが、値を指定することもできます。 指定したキーが見つからないために返されるキーとnull、指定したキーのnull値が原因でOrderedDictionary返されるキーを区別nullするには、メソッドをContains使用してキーが存在するかどうかを判断します。

適用対象