IDictionary<TKey,TValue>.Add(TKey, TValue) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Aggiunge un elemento con la chiave e il IDictionary<TKey,TValue>valore specificati a .
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)
Parametri
- key
- TKey
Oggetto da utilizzare come chiave dell'elemento da aggiungere.
- value
- TValue
Oggetto da utilizzare come valore dell'elemento da aggiungere.
Eccezioni
key è null.
Un elemento con la stessa chiave esiste già in IDictionary<TKey,TValue>.
è IDictionary<TKey,TValue> di sola lettura.
Esempio
L'esempio di codice seguente crea un vuoto Dictionary<TKey,TValue> di stringhe, con chiavi integer e vi accede tramite l'interfaccia IDictionary<TKey,TValue> . Nell'esempio di codice viene utilizzato il Add metodo per aggiungere alcuni elementi. Nell'esempio viene illustrato che il Add metodo genera un'eccezione ArgumentException quando si tenta di aggiungere una chiave duplicata.
Questo codice fa parte di un esempio più ampio che può essere compilato ed eseguito. Vedete System.Collections.Generic.IDictionary<TKey,TValue>.
// 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
Commenti
È anche possibile utilizzare la proprietà Item[] per aggiungere nuovi elementi impostando il valore di una chiave che non esiste nel dizionario; ad esempio myCollection["myNonexistentKey"] = myValue in C# (myCollection("myNonexistentKey") = myValue in Visual Basic). Tuttavia, se la chiave specificata esiste già nel dizionario, l'impostazione della Item[] proprietà sovrascrive il valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti.
Le implementazioni possono variare in base al modo in cui determinano l'uguaglianza degli oggetti; Ad esempio, la List<T> classe usa Comparer<T>.Default, mentre la Dictionary<TKey,TValue> classe consente all'utente di specificare l'implementazione IComparer<T> da usare per confrontare le chiavi.
Le implementazioni possono variare se consentono key di essere null.