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

定义

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

用作要添加的元素的键的对象。

value
TValue

用作要添加的元素的值的对象。

例外

keynull

IDictionary<TKey,TValue> 中已存在具有相同键的元素。

示例

下面的代码示例使用整数键创建一个空 Dictionary<TKey,TValue> 字符串,并通过 IDictionary<TKey,TValue> 接口访问它。 代码示例使用 Add 方法添加一些元素。 该示例演示方法在 Add 尝试添加重复键时引发 ArgumentException

此代码是可以编译和执行的更大示例的一部分。 请参阅 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[] 属性通过设置字典中不存在的键的值来添加新元素;例如, myCollection["myNonexistentKey"] = myValue 在 C# (myCollection("myNonexistentKey") = myValue Visual Basic) 。 但是,如果字典中已存在指定的键,则设置 Item[] 属性将覆盖旧值。 相反, Add 方法不修改现有元素。

实现在确定对象相等性的方式上可能会有所不同;例如, List<T> 类使用 Comparer<T>.Default,而 Dictionary<TKey,TValue> 类允许用户指定 IComparer<T> 用于比较键的实现。

实现在是否允许 keynull方面可能会有所不同。

适用于

另请参阅