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

定義

將有指定索引鍵和數值的項目加入 SortedList<TKey,TValue>Adds an element with the specified key and value into the 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)

參數

key
TKey TKey TKey TKey

要加入的項目的索引鍵。The key of the element to add.

value
TValue TValue TValue TValue

要加入的項目的值。The value of the element to add. 參考類型的值可以是 nullThe value can be null for reference types.

實作

例外狀況

SortedList<TKey,TValue> 中已存在具有相同索引鍵的元素。An element with the same key already exists in the SortedList<TKey,TValue>.

範例

下列程式碼範例會建立具有SortedList<TKey,TValue>字串索引鍵的空字串,並Add使用方法來新增一些元素。The following code example creates an empty SortedList<TKey,TValue> of strings with string keys and uses the Add method to add some elements. 這個範例會示範Add ArgumentException當嘗試加入重複的索引鍵時,方法會擲回。The example demonstrates that the Add method throws an ArgumentException when attempting to add a duplicate key.

這個程式碼範例是針對SortedList<TKey,TValue>類別提供之較大範例的一部分。This code example is part of a larger example provided for the SortedList<TKey,TValue> class.

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

備註

索引鍵不能null是,但如果已排序TValue清單中的數值型別是參考型別,則值可以是。A key cannot be null, but a value can be, if the type of values in the sorted list, TValue, is a reference type.

您也可以使用Item[TKey]屬性來加入新的專案,方法是設定不存在SortedList<TKey,TValue>于中之索引鍵的值, myCollection["myNonexistentKey"] = myValue例如。You can also use the Item[TKey] property to add new elements by setting the value of a key that does not exist in the SortedList<TKey,TValue>; for example, myCollection["myNonexistentKey"] = myValue. 不過,如果指定的索引鍵已存在於SortedList<TKey,TValue>中,則Item[TKey]設定屬性會覆寫舊值。However, if the specified key already exists in the SortedList<TKey,TValue>, setting the Item[TKey] property overwrites the old value. 相反地, Add方法不會修改現有的元素。In contrast, the Add method does not modify existing elements.

如果Count已經等於Capacity ,則會藉由自動重新配置內部陣列來增加的容量,並將現有的專案複製到新的陣列中,然後再加入新的元素。SortedList<TKey,TValue>If Count already equals Capacity, the capacity of the SortedList<TKey,TValue> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

這個方法是未排序資料n的 O ()運算,其中nCountThis method is an O(n) operation for unsorted data, where n is Count. 如果新的專案新增至n清單結尾,這就是 O (log)作業。It is an O(log n) operation if the new element is added at the end of the list. 如果插入造成調整大小,則作業為 On()。If insertion causes a resize, the operation is O(n).

適用於

另請參閱