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

Definicja

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

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 SortedList<TKey,TValue>.

Przykłady

Poniższy przykład kodu tworzy pusty SortedList<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 SortedList<TKey,TValue> dla klasy.

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

Uwagi

Kluczem nie może być , ale wartość może być null, jeśli typ wartości na posortowanej liście , TValuejest typem odwołania.

Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w elemecie SortedList<TKey,TValue>, na przykład myCollection["myNonexistentKey"] = myValue. Jeśli jednak określony klucz już istnieje w obiekcie SortedList<TKey,TValue>, ustawienie Item[] właściwości zastępuje starą wartość. Add Natomiast metoda nie modyfikuje istniejących elementów.

Jeśli Count już równa Capacitysię , pojemność SortedList<TKey,TValue> obiektu jest zwiększana przez automatyczne cofnięcie przydziału tablicy wewnętrznej, a istniejące elementy są kopiowane do nowej tablicy przed dodaniu nowego elementu.

Ta metoda jest operacją O(n) dla nieortowanych danych, gdzie n to Count. Jest to operacja O(log n), jeśli nowy element zostanie dodany na końcu listy. Jeśli wstawienie powoduje zmianę rozmiaru, operacja to O(n).

Dotyczy

Zobacz też