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

Definition

Lägger till den angivna nyckeln och värdet i ordlistan.

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)

Parametrar

key
TKey

Nyckeln för elementet som ska läggas till.

value
TValue

Värdet för elementet som ska läggas till. Värdet kan vara null för referenstyper.

Implementeringar

Undantag

key är null.

Det finns redan ett element med samma nyckel i Dictionary<TKey,TValue>.

Exempel

I följande kodexempel skapas en tom Dictionary<TKey,TValue> sträng med strängnycklar och metoden används Add för att lägga till vissa element. Exemplet visar att Add metoden genererar en ArgumentException när du försöker lägga till en dubblettnyckel.

Det här kodexemplet är en del av ett större exempel för Dictionary<TKey,TValue> klassen.

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

Kommentarer

Du kan också använda egenskapen Item[] för att lägga till nya element genom att ange värdet för en nyckel som inte finns i Dictionary<TKey,TValue>. till exempel myCollection[myKey] = myValue (i Visual Basic myCollection(myKey) = myValue). Men om den angivna nyckeln redan finns i Dictionary<TKey,TValue>, skriver egenskapen Item[] över det gamla värdet. Metoden genererar däremot Add ett undantag om det redan finns ett värde med den angivna nyckeln.

Count Om egenskapsvärdet redan är lika med kapaciteten ökas kapaciteten Dictionary<TKey,TValue> för den genom att den interna matrisen automatiskt omplaceras och befintliga element kopieras till den nya matrisen innan det nya elementet läggs till.

En nyckel kan inte vara null, men ett värde kan vara, om TValue det är en referenstyp.

Om Count är mindre än kapaciteten närmar sig den här metoden en O(1)-åtgärd. Om kapaciteten måste ökas för att rymma det nya elementet blir den här metoden en O(n)-åtgärd, där n är Count.

Gäller för

Se även