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. 此範例示範Add方法會擲回ArgumentException時嘗試要加入重複的索引鍵。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"] = myValueC# 中 (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.

實作有所不同是否允許keynullImplementations can vary in whether they allow key to be null.

適用於

另請參閱