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

Definicja

Dodaje określony klucz i wartość do słownika.

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

Klucz elementu do dodania.

value
TValue

Wartość elementu do dodania. Wartość może być null przeznaczona dla typów referencyjnych.

Implementuje

Wyjątki

key to null.

Element o tym samym kluczu już istnieje w pliku Dictionary<TKey,TValue>.

Przykłady

Poniższy przykład kodu tworzy pusty Dictionary<TKey,TValue> ciąg z kluczami ciągów i używa Add metody , aby dodać niektóre elementy. W przykładzie pokazano, że Add metoda zgłasza ArgumentException błąd podczas próby dodania zduplikowanego klucza.

Ten przykład kodu jest częścią większego przykładu podanego Dictionary<TKey,TValue> dla klasy.

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

Uwagi

Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w obiekcie Dictionary<TKey,TValue>, na przykład myCollection[myKey] = myValue (w Visual Basic, myCollection(myKey) = myValue). Jeśli jednak określony klucz już istnieje w obiekcie Dictionary<TKey,TValue>, ustawienie Item[] właściwości zastępuje starą wartość. Natomiast metoda zgłasza wyjątek, Add jeśli wartość z określonym kluczem już istnieje.

Count Jeśli wartość właściwości jest już równa pojemności, pojemność Dictionary<TKey,TValue> jest zwiększana przez automatyczne przydzielenie tablicy wewnętrznej, a istniejące elementy są kopiowane do nowej tablicy przed dodaniu nowego elementu.

Kluczem nie może być null, ale wartość może być, jeśli TValue jest typem odwołania.

Jeśli Count jest mniejsza niż pojemność, ta metoda zbliża się do operacji O(1). Jeśli pojemność musi zostać zwiększona, aby pomieścić nowy element, ta metoda staje się operacją O(n), gdzie n to Count.

Dotyczy

Zobacz też