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

定义

IDictionary<TKey,TValue> 中添加一个带有所提供的键和值的元素。Adds an element with the provided key and value to the IDictionary<TKey,TValue>.

public:
 void Add(TKey key, TValue value);
public void Add (TKey key, TValue value);
abstract member Add : 'Key * 'Value -> unit
Public Sub Add (key As TKey, value As TValue)

参数

key
TKey TKey TKey TKey

用作要添加的元素的键的对象。The object to use as the key of the element to add.

value
TValue TValue TValue TValue

用作要添加的元素的值的对象。The object to use as the value of the element to add.

异常

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

示例

下面的代码示例创建一个空Dictionary<TKey,TValue>的字符串,其中包含整数键,并IDictionary<TKey,TValue>通过接口进行访问。The following code example creates an empty Dictionary<TKey,TValue> of strings, with integer keys, and accesses it through the IDictionary<TKey,TValue> interface. 此代码示例使用Add方法添加一些元素。The code example 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.

此代码是可以编译和执行的更大示例的一部分。This code is part of a larger example that can be compiled and executed. 请参阅 System.Collections.Generic.IDictionary<TKey,TValue>See System.Collections.Generic.IDictionary<TKey,TValue>.

// Create a new dictionary of strings, with string keys,
// and access it through the IDictionary generic interface.
IDictionary<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, 
// and access it through the IDictionary generic interface.
IDictionary<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, 
' and access it through the IDictionary generic interface.
Dim openWith As IDictionary(Of String, String) = _
    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]还可以使用属性来添加新元素,方法是设置字典中不存在的键的值; 例如, myCollection["myNonexistentKey"] = myValue在中C# (myCollection("myNonexistentKey") = myValue Visual Basic)。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; for example, myCollection["myNonexistentKey"] = myValue in C# (myCollection("myNonexistentKey") = myValue in Visual Basic). 但是,如果字典中已存在指定的键,则设置Item[TKey]属性会覆盖旧值。However, if the specified key already exists in the dictionary, setting the Item[TKey] property overwrites the old value. 与此Add相反,方法不会修改现有元素。In contrast, the Add method does not modify existing elements.

实现在确定对象的相等性方面可能有所不同;例如, List<T>类使用Comparer<T>.Default,而Dictionary<TKey,TValue>类允许用户指定IComparer<T>要用于比较键的实现。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

实现在是否允许key null时可能有所不同。Implementations can vary in whether they allow key to be null.

适用于

另请参阅