IDictionary<TKey,TValue>.Add(TKey, TValue) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將隨附有索引鍵和值的項目加入至 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
做為要加入項目之值的物件。
例外狀況
key
為 null
。
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
在 Visual Basic) 的 C# (myCollection("myNonexistentKey") = myValue
中。 不過,如果指定的索引鍵已存在於字典中,則設定 Item[] 屬性會覆寫舊的值。 相反地, Add 方法不會修改現有的專案。
實作可能會因判斷物件的相等方式而有所不同;例如, List<T> 類別會使用 Comparer<T>.Default,而 Dictionary<TKey,TValue> 類別可讓使用者指定要 IComparer<T> 用於比較索引鍵的實作。
實作可能會因是否允許 key
為 null
而有所不同。