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

定义

将带有指定键和值的元素添加到 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

要添加的元素的键。The key of the element to add.

value
TValue

要添加的元素的值。The value of the element to add. 对于引用类型,该值可以为 nullThe value can be null for reference types.

实现

异常

keynullkey is null.

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. 该示例演示AddArgumentException在尝试添加重复键时方法引发。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"] = myValueYou 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> equals,则通过自动重新分配内部数组,并将现有元素复制到新数组,然后再添加新元素。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 ()运算, n其中Count是。This 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).

适用于

另请参阅