PropertyCollection.IDictionary.Add(Object, Object) メソッド

定義

指定したキーおよび値を持つ要素を IDictionary オブジェクトに追加します。

 virtual void System.Collections.IDictionary.Add(System::Object ^ key, System::Object ^ value) = System::Collections::IDictionary::Add;
void IDictionary.Add (object key, object value);
abstract member System.Collections.IDictionary.Add : obj * obj -> unit
override this.System.Collections.IDictionary.Add : obj * obj -> unit
Sub Add (key As Object, value As Object) Implements IDictionary.Add

パラメーター

key
Object

追加する要素のキーとして使用する Object

value
Object

追加する要素の値として使用する Object

実装

例外

keynullです。

同じキーを持つ要素が、IDictionary オブジェクトに既に存在します。

IDictionary は読み取り専用です。

- または -

IDictionary は固定サイズです。

次の例は、Add メソッドを実装する方法を示しています。 このコード例は、IDictionary クラスのために提供されている大規模な例の一部です。

public:
    virtual void Add(Object^ key, Object^ value)
    {
        // Add the new key/value pair even if this key already exists
        // in the dictionary.
        if (itemsInUse == items->Length)
        {
            throw gcnew InvalidOperationException
                ("The dictionary cannot hold any more items.");
        }
        items[itemsInUse++] = gcnew DictionaryEntry(key, value);
    }
public void Add(object key, object value)
{
    // Add the new key/value pair even if this key already exists in the dictionary.
    if (ItemsInUse == items.Length)
        throw new InvalidOperationException("The dictionary cannot hold any more items.");
    items[ItemsInUse++] = new DictionaryEntry(key, value);
}
Public Sub Add(ByVal key As Object, ByVal value As Object) Implements IDictionary.Add

    ' Add the new key/value pair even if this key already exists in the dictionary.
    If ItemsInUse = items.Length Then
        Throw New InvalidOperationException("The dictionary cannot hold any more items.")
    End If
    items(ItemsInUse) = New DictionaryEntry(key, value)
    ItemsInUse = ItemsInUse + 1
End Sub

注釈

プロパティを Item[] 使用して、ディクショナリに存在しないキーの値 (例: ) を設定して、 myCollection["myNonexistentKey"] = myValue新しい要素を追加することもできます。 ただし、指定したキーがディクショナリに既に存在する場合は、 プロパティを Item[] 設定すると古い値が上書きされます。 これに対し、 メソッドは既存の Add 要素を変更しません。

適用対象

こちらもご覧ください