IDictionary<TKey,TValue>.Add(TKey, TValue) 메서드

정의

제공된 키와 값을 가진 요소를 IDictionary<TKey,TValue>에 추가합니다.

public:
 void Add(TKey key, TValue value);
public void Add (TKey key, TValue value);
abstract member Add : 'Key * 'Value -> unit
Public Sub Add (key As TKey, value As TValue)

매개 변수

key
TKey

추가할 요소의 키로 사용할 개체입니다.

value
TValue

추가할 요소의 값으로 사용할 개체입니다.

예외

key이(가) null인 경우

같은 키를 가진 요소가 이미 IDictionary<TKey,TValue>에 있는 경우

IDictionary<TKey,TValue>이 읽기 전용인 경우

예제

다음 코드 예제에서는 정수 키를 사용하여 빈 Dictionary<TKey,TValue> 문자열을 만들고 인터페이스를 IDictionary<TKey,TValue> 통해 액세스합니다. 코드 예제에서는 메서드를 Add 사용하여 일부 요소를 추가합니다. 이 예제에서는 메서드가 Add 중복 키를 추가하려고 할 때 을 throw ArgumentException 하는 것을 보여 줍니다.

이 코드는 컴파일 및 실행할 수 있는 더 큰 예제의 일부입니다. System.Collections.Generic.IDictionary<TKey,TValue>을 참조하세요.

// Create a new dictionary of strings, with string keys,
// and access it through the IDictionary generic interface.
IDictionary<String^, String^>^ openWith =
    gcnew Dictionary<String^, String^>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith->Add("txt", "notepad.exe");
openWith->Add("bmp", "paint.exe");
openWith->Add("dib", "paint.exe");
openWith->Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new dictionary of strings, with string keys,
// and access it through the IDictionary generic interface.
IDictionary<string, string> openWith =
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new dictionary of strings, with string keys, 
' and access it through the IDictionary generic interface.
Dim openWith As IDictionary(Of String, String) = _
    New Dictionary(Of String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

설명

사용할 수도 있습니다는 Item[] 속성을 설정 하 여 새 요소를 추가할 수 있습니다는 사전에 없는 키의 값을 설정 하 여(예 myCollection["myNonexistentKey"] = myValue : C# (myCollection("myNonexistentKey") = myValue Visual Basic에서). 그러나 지정된 키가 사전에 이미 있는 경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면, 메서드는 Add 기존 요소를 수정하지 않습니다.

구현은 개체의 같음을 결정하는 방법에 따라 달라질 수 있습니다. 예를 들어 클래스는 List<T> 를 사용하는 Comparer<T>.Default반면 Dictionary<TKey,TValue> , 클래스를 사용하면 사용자가 키를 비교하는 데 사용할 구현을 지정할 IComparer<T> 수 있습니다.

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

적용 대상

추가 정보