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

Definizione

Aggiunge un elemento con la chiave e il valore specificati al metodo 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)

Parametri

key
TKey

Chiave dell'elemento da aggiungere.

value
TValue

Valore dell'elemento da aggiungere. Il valore può essere null per i tipi di riferimento.

Implementazioni

Eccezioni

key è null.

In SortedList<TKey,TValue> è già presente un elemento con la stessa chiave.

Esempio

Nell'esempio di codice seguente viene creato un vuoto SortedList<TKey,TValue> di stringhe con chiavi di stringa e viene usato il Add metodo per aggiungere alcuni elementi. Nell'esempio viene illustrato che il Add metodo genera un oggetto ArgumentException quando si tenta di aggiungere una chiave duplicata.

Questo esempio di codice fa parte di un esempio più grande fornito per la SortedList<TKey,TValue> classe.

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

Commenti

Una chiave non può essere , ma un valore può essere null, se il tipo di valori nell'elenco ordinato, TValueè un tipo di riferimento.

È anche possibile usare la Item[] proprietà per aggiungere nuovi elementi impostando il valore di una chiave che non esiste nell'oggetto SortedList<TKey,TValue>, ad esempio myCollection["myNonexistentKey"] = myValue. Tuttavia, se la chiave specificata esiste già nell'impostazione SortedList<TKey,TValue>, impostando la proprietà sovrascrive il Item[] valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti.

Se Count è già uguale Capacity, la capacità dell'oggetto SortedList<TKey,TValue> viene aumentata automaticamente riallocare la matrice interna e gli elementi esistenti vengono copiati nella nuova matrice prima dell'aggiunta del nuovo elemento.

Questo metodo è un'operazione O(n) per i dati non autorizzati, dove n è Count. Si tratta di un'operazione O(log n) se il nuovo elemento viene aggiunto alla fine dell'elenco. Se l'inserimento causa un ridimensionamento, l'operazione è O(n).

Si applica a

Vedi anche