Sdílet prostřednictvím


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 prvku, 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ž v objektu Dictionary<TKey,TValue>.

Příklady

Následující příklad kódu vytvoří prázdné Dictionary<TKey,TValue> řetězce 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á 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 uvedeného pro třídu Dictionary<TKey,TValue>.

// 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.
let openWith = 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")
with :? ArgumentException ->
    printfn "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 také použít Item[] k přidání nových prvků nastavením hodnoty klíče, který neexistuje v objektu Dictionary<TKey,TValue>, například myCollection[myKey] = myValue (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 hodnota se zadaným klíčem již existuje.

Count Pokud se hodnota vlastnosti již rovná kapacitě, kapacita objektu Dictionary<TKey,TValue> je zvýšena automaticky přidělení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 typ odkazu.

Pokud Count je menší než kapacita, tato metoda přistupuje k operaci O(1). Pokud je nutné zvýšit kapacitu tak, aby vyhovovala novému prvku, tato metoda se stane operací O(n), kde n je Count.

Platí pro

Viz také