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

Definice

Přidá prvek se zadaným klíčem a hodnotou do objektu SortedList<TKey,TValue>.

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íč prvku, který chcete přidat.

value
TValue

Hodnota elementu, který chcete přidat Hodnota může být null pro referenční typy.

Implementuje

Výjimky

key je null.

Prvek se stejným klíčem již existuje v objektu SortedList<TKey,TValue>.

Příklady

Následující příklad kódu vytvoří prázdný SortedList<TKey,TValue> řetězec 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á chybu 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 SortedList<TKey,TValue> pro třídu.

// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
    gcnew SortedList<String^, String^>();

// Add some elements to the list. 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 list.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
    new SortedList<string, string>();

// Add some elements to the list. 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 list.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string 
' keys. 
Dim openWith As New SortedList(Of String, String)

' Add some elements to the list. 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 list.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
let openWith = SortedList<string, string>()

// Add some elements to the list. 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 list.
try
    openWith.Add("txt", "winword.exe");
with
    | :? ArgumentException ->
        printfn "An element with Key = \"txt\" already exists."

Poznámky

Klíč nemůže být null, ale hodnota může být, pokud typ hodnot v seřazených seznamu TValueje typ odkazu.

Vlastnost můžete také použít Item[] k přidání nových prvků nastavením hodnoty klíče, který v objektu SortedList<TKey,TValue>neexistuje, myCollection["myNonexistentKey"] = myValuenapříklad . Pokud však zadaný klíč již existuje v SortedList<TKey,TValue>, nastavení Item[] vlastnosti přepíše starou hodnotu. Naproti tomu Add metoda neupravuje existující prvky.

Pokud Count se již rovná Capacity, kapacita objektu SortedList<TKey,TValue> se zvýší automatickým opětovným 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.

Tato metoda je operace O(n) pro neseřazená data, kde n je Count. Jedná se o operaci O(log n), pokud je nový prvek přidán na konec seznamu. Pokud vložení způsobí změnu velikosti, operace je O(n).

Platí pro

Viz také