IDictionary.Add(Object, Object) 메서드

정의

제공된 키와 값을 가진 요소를 IDictionary 개체에 추가합니다.

public:
 void Add(System::Object ^ key, System::Object ^ value);
public void Add (object key, object value);
public void Add (object key, object? value);
abstract member Add : obj * obj -> unit
Public Sub Add (key As Object, value As Object)

매개 변수

key
Object

추가할 요소의 키로 사용하는 Object입니다.

value
Object

추가할 요소의 값으로 사용하는 Object입니다.

예외

key이(가) null인 경우

같은 키를 가진 요소가 이미 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 기존 요소를 수정하지 않습니다.

구현은 키가 가 되도록 허용할지 여부에 따라 달라질 수 있습니다 null.

적용 대상

추가 정보