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

定义

将指定的键和值添加到字典中。Adds the specified key and value to the dictionary.

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.

实现

异常

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

示例

备注

本文中的 C# 示例运行在 Try.NET 内联代码运行程序和演练环境中。The C# examples in this article run in the Try.NET inline code runner and playground. 选择“运行”按钮以在交互窗口中运行示例 。Select the Run button to run an example in an interactive window. 执行代码后,可通过再次选择“运行”来修改它并运行已修改的代码 。Once you execute the code, you can modify it and run the modified code by selecting Run again. 已修改的代码要么在交互窗口中运行,要么编译失败时,交互窗口将显示所有 C# 编译器错误消息。The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

下面的代码示例使用字符串键Dictionary<TKey,TValue>创建一个空的字符串,并Add使用方法来添加一些元素。The following code example creates an empty Dictionary<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.

此代码示例是为Dictionary<TKey,TValue>类提供的更大示例的一部分。This code example is part of a larger example provided for the Dictionary<TKey,TValue> class.

// Create a new dictionary of strings, with string keys.
//
Dictionary<String^, String^>^ openWith =
    gcnew Dictionary<String^, String^>();

// Add some elements to the dictionary. 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 dictionary.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. 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 dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new dictionary of strings, with string keys.
'
Dim openWith As New Dictionary(Of String, String)

' Add some elements to the dictionary. 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 dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

注解

Item[TKey]你还可以使用属性来添加新元素,方法是设置Dictionary<TKey,TValue>中不存在的键的值; 例如myCollection[myKey] = myValue (在 Visual Basic myCollection(myKey) = 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 Dictionary<TKey,TValue>; for example, myCollection[myKey] = myValue (in Visual Basic, myCollection(myKey) = myValue). 但是,如果指定的键已存在于中Dictionary<TKey,TValue>,则Item[TKey]设置属性会覆盖旧值。However, if the specified key already exists in the Dictionary<TKey,TValue>, setting the Item[TKey] property overwrites the old value. 与此相反, Add如果具有指定键的值已存在,该方法将引发异常。In contrast, the Add method throws an exception if a value with the specified key already exists.

如果该Count属性值已等于容量, Dictionary<TKey,TValue>则会自动重新分配内部数组,并将现有元素复制到新数组,然后再添加新的元素。If the Count property value already equals the capacity, the capacity of the Dictionary<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.

null如果TValue是引用类型,则键不能为,但值可以为。A key cannot be null, but a value can be, if TValue is a reference type.

如果Count小于容量,则此方法将使用 O (1)操作。If Count is less than the capacity, this method approaches an O(1) operation. 如果必须增加容量以容纳新元素,则此方法将成为 O (n)运算,其中nCountIf the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

适用于

另请参阅