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 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 puste SortedList<TKey,TValue> ciągi z kluczami ciągów i używa Add metody w celu dodania niektórych elementów. W przykładzie Add pokazano, że metoda zgłasza błąd ArgumentException podczas próby dodania zduplikowanego klucza.

Ten przykład kodu jest częścią większego przykładu udostępnionego 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
// 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."

Uwagi

Klucz nie może być nullwartością , ale wartość może być, jeśli typ wartości na posortowanej liście, TValue, jest typem referencyjnym.

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 SortedList<TKey,TValue>obiekcie , ustawienie Item[] właściwości zastępuje starą wartość. Z kolei Add metoda nie modyfikuje istniejących elementów.

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

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

Dotyczy

Zobacz też