Dictionary<TKey,TValue>.Add(TKey, TValue) Metoda

Definice

Přidá zadaný klíč a hodnotu do slovníku.

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)

Parametry

key
TKey

Klíč elementu, který chcete přidat.

value
TValue

Hodnota elementu, který chcete přidat Hodnota může být null pro odkazové typy.

Implementuje

Výjimky

key je null.

Prvek se stejným klíčem již existuje v objektu Dictionary<TKey,TValue>.

Příklady

Následující příklad kódu vytvoří prázdný Dictionary<TKey,TValue> řetězec s řetězcovými klíči a použije metodu Add k přidání některých prvků. Příklad ukazuje, že Add metoda vyvolá výjimku ArgumentException při pokusu o přidání duplicitního klíče.

Tento příklad kódu je součástí většího příkladu Dictionary<TKey,TValue> pro třídu .

// 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

Poznámky

Vlastnost můžete použít také Item[] k přidání nových prvků nastavením hodnoty klíče, myCollection[myKey] = myValue který neexistuje v Dictionary<TKey,TValue>, například (v jazyce Visual Basic, myCollection(myKey) = myValue). Pokud však zadaný klíč již existuje v Dictionary<TKey,TValue>, nastavení Item[] vlastnosti přepíše starou hodnotu. Naproti tomu metoda vyvolá výjimku, Add pokud již existuje hodnota se zadaným klíčem.

Count Pokud se hodnota vlastnosti již rovná kapacitě, kapacita objektu Dictionary<TKey,TValue> se zvýší automatickým přerozdělováním interního pole a existující prvky se zkopírují do nového pole před přidáním nového prvku.

Klíč nemůže být null, ale hodnota může být, pokud TValue je odkazovým typem.

Pokud Count je menší než kapacita, tato metoda se blíží operaci O(1). Pokud je potřeba kapacitu zvýšit, aby vyhovovala novému prvku, stane se z této metody operace O(n), kde n je Count.

Platí pro

Viz také