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

定義

將具有提供索引鍵和值的元素加入至 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

做為要加入項目之索引鍵的物件。The object to use as the key of the element to add.

value
TValue

做為要加入項目之值的物件。The object to use as the value of the element to add.

例外狀況

keynullkey is null.

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"] = 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.

適用於

另請參閱