OrderedDictionary.Item[] プロパティ

定義

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

オーバーロード

名前 説明
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

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

プロパティ値

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

実装

例外

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

index が 0 未満です。

-又は-

indexCount 以上です。

注釈

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

C# 言語では、 プロパティを実装する代わりに、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に存在するかどうかを判断します。

適用対象