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

Definicja

Dodaje element z podanym kluczem i wartością do elementu 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)

Parametry

key
TKey

Obiekt, który ma być używany jako klucz elementu do dodania.

value
TValue

Obiekt, który ma być używany jako wartość elementu do dodania.

Wyjątki

key to null.

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

Element IDictionary<TKey,TValue> jest tylko do odczytu.

Przykłady

Poniższy przykład kodu tworzy pusty Dictionary<TKey,TValue> ciąg z kluczami całkowitymi i uzyskuje do niego dostęp za pośrednictwem interfejsu IDictionary<TKey,TValue> . W przykładzie kodu użyto 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 kod jest częścią większego przykładu, który można skompilować i wykonać. Zobacz: .

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

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 słowniku, na przykład myCollection["myNonexistentKey"] = myValue w języku C# (myCollection("myNonexistentKey") = myValue w Visual Basic). Jeśli jednak określony klucz już istnieje w słowniku, ustawienie Item[] właściwości zastępuje starą wartość. Add Natomiast metoda nie modyfikuje istniejących elementów.

Implementacje mogą różnić się w sposobie określania równości obiektów; na przykład List<T> klasa używa klasy Comparer<T>.Default, natomiast Dictionary<TKey,TValue> klasa umożliwia użytkownikowi określenie IComparer<T> implementacji do użycia do porównywania kluczy.

Implementacje mogą się różnić w zależności od tego, czy zezwalają key na wartość null.

Dotyczy

Zobacz też