共用方式為


SortedList<TKey,TValue>.Add(TKey, TValue) 方法

定義

將有指定索引鍵和數值的項目加入 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

要加入的項目的索引鍵。

value
TValue

要加入的項目的值。 參考類型的值可以是 null

實作

例外狀況

keynull

SortedList<TKey,TValue> 中已存在具有相同索引鍵的元素。

範例

下列程式代碼範例會使用字串索引鍵建立空 SortedList<TKey,TValue> 字串,並使用 Add 方法來加入某些元素。 此範例示範 Add 嘗試加入重複索引鍵時,方法會擲回 ArgumentException

此程式代碼範例是提供給 類別之較大範例的 SortedList<TKey,TValue> 一部分。

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

備註

索引鍵不能是 null,但如果排序列表中的值類型是參考型別, TValue則值可以是 。

您也可以藉由設定不存在於 中的SortedList<TKey,TValue>索引鍵值,來使用 Item[] 屬性來加入新元素,例如 myCollection["myNonexistentKey"] = myValue。 不過,如果指定的索引鍵已存在於 中 SortedList<TKey,TValue>,則設定 Item[] 屬性會覆寫舊值。 相反地, Add 方法不會修改現有的專案。

如果 Count 已經等於 Capacity,則會藉由自動重新配置內部陣列來增加的 SortedList<TKey,TValue> 容量,而現有的元素會在加入新元素之前複製到新的陣列。

這個方法是未排序資料的 O (n) 作業,其中 nCount。 如果在清單結尾新增了新元素,則它是 O (記錄 n) 作業。 如果插入會導致重設大小,作業會是 O (n) 。

適用於

另請參閱