IDictionary.Add(Object, Object) 메서드
정의
제공된 키와 값을 가진 요소를 IDictionary 개체에 추가합니다.Adds an element with the provided key and value to the IDictionary object.
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
이(가) null
인 경우key
is null
.
같은 키를 가진 요소가 이미 IDictionary 개체에 있는 경우An element with the same key already exists in the IDictionary object.
IDictionary이 읽기 전용인 경우The IDictionary is read-only.
또는-or- IDictionary가 고정 크기입니다.The IDictionary has a fixed size.
예제
다음 코드 예제를 구현 하는 방법에 설명 합니다 Add 메서드.The following code example demonstrates how to implement the Add method. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 IDictionary 클래스입니다.This code example is part of a larger example provided for the IDictionary class.
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
.You can also use the Item[] property to add new elements by setting the value of a key that does not exist in the dictionary (for example, myCollection["myNonexistentKey"] = myValue
). 그러나 지정 된 키가 사전에 이미 있는 경우 속성을 설정 하면 Item[] 이전 값을 덮어씁니다.However, if the specified key already exists in the dictionary, setting the Item[] property overwrites the old value. 반면, 메서드는 Add 기존 요소를 수정 하지 않습니다.In contrast, the Add method does not modify existing elements.
구현은 키를 허용 하는지 여부에 따라 달라질 수 있습니다 null
.Implementations can vary in whether they allow the key to be null
.