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

정의

지정한 키와 값을 사전에 추가합니다.

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

매개 변수

key
TKey

추가할 요소의 키입니다.

value
TValue

추가할 요소의 값입니다. 참조 형식에 대해 값은 null이 될 수 있습니다.

구현

예외

key이(가) null인 경우

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

예제

다음 코드 예제에서는 문자열 키가 있는 빈 Dictionary<TKey,TValue> 문자열을 만들고 메서드를 Add 사용하여 일부 요소를 추가합니다. 이 예제에서는 Add 중복 키를 추가하려고 할 때 메서드가 throw ArgumentException 되는 것을 보여 줍니다.

이 코드 예제는에 대해 제공 된 큰 예제의 일부는 Dictionary<TKey,TValue> 클래스입니다.

// Create a new dictionary of strings, with string keys.
//
Dictionary<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.
//
Dictionary<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.
'
Dim openWith As 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[] 없는 Dictionary<TKey,TValue>키의 값을 설정 하 여 새 요소를 추가 하는 속성을 사용할 수도 있습니다., myCollection[myKey] = myValue 예를 들어 (Visual Basic, myCollection(myKey) = myValue). 그러나 지정된 키가 이미 있는 Dictionary<TKey,TValue>경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면 지정된 Add 키를 가진 값이 이미 있는 경우 메서드는 예외를 throw합니다.

속성 값이 Count 이미 용량과 같으면 내부 배열을 자동으로 다시 할당하여 용량 Dictionary<TKey,TValue> 이 증가하고 새 요소가 추가되기 전에 기존 요소가 새 배열에 복사됩니다.

키는 사용할 null수 없지만 참조 형식인 경우 TValue 값이 될 수 있습니다.

용량보다 작은 경우 Count 이 메서드는 O(1) 작업에 접근합니다. 새 요소를 수용하기 위해 용량을 늘려야 하는 경우 이 메서드는 O(n) 작업이 n Count됩니다.

적용 대상

추가 정보