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则值可以是 。

还可以使用 Item[] 属性通过设置 中 SortedList<TKey,TValue>不存在的键的值来添加新元素, myCollection["myNonexistentKey"] = myValue例如 。 但是,如果 指定的键已存在于 中 SortedList<TKey,TValue>,设置 Item[] 属性将覆盖旧值。 相反, Add 方法不修改现有元素。

如果 Count 已等于 Capacity,则通过自动重新分配内部数组来增加 的容量 SortedList<TKey,TValue> ,并在添加新元素之前将现有元素复制到新数组。

此方法是针对未排序数据的 O (n) 操作,其中 nCount。 如果在列表末尾添加新元素,则此操作是 O (日志 n) 操作。 如果插入导致重设大小,则操作为 O (n) 。

适用于

另请参阅