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

定義

將指定的索引鍵和值加入字典。Adds the specified key and value to the dictionary.

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

參數

key
TKey TKey TKey TKey

要加入的項目的索引鍵。The key of the element to add.

value
TValue TValue TValue TValue

要加入的項目的值。The value of the element to add. 參考類型的值可以是 nullThe value can be null for reference types.

實作

例外狀況

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

範例

下列程式碼範例會建立空Dictionary<TKey,TValue>之字串的字串索引鍵,並使用Add方法來加入一些項目。The following code example creates an empty Dictionary<TKey,TValue> of strings with string keys and 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.

此程式碼範例是針對提供之較大範例的一部分Dictionary<TKey,TValue>類別。This code example is part of a larger example provided for the Dictionary<TKey,TValue> class.

// Create a new dictionary of strings, with string keys.
//
Dictionary<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.
//
Dictionary<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.
'
Dim openWith As 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]屬性來設定索引鍵的值加入新項目不存在於Dictionary<TKey,TValue>,例如myCollection[myKey] = myValue(在 Visual Basic 中myCollection(myKey) = myValue)。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<TKey,TValue>; for example, myCollection[myKey] = myValue (in Visual Basic, myCollection(myKey) = myValue). 不過,如果指定的索引鍵已存在於Dictionary<TKey,TValue>,將Item[TKey]屬性會覆寫舊的值。However, if the specified key already exists in the Dictionary<TKey,TValue>, setting the Item[TKey] property overwrites the old value. 相反地,Add方法擲回例外狀況,如果已經存在具有指定之索引鍵的值。In contrast, the Add method throws an exception if a value with the specified key already exists.

如果Count屬性值已等於容量的容量Dictionary<TKey,TValue>會加上自動重新配置內部的陣列,並加入新項目之前,現有的項目會複製到新陣列。If the Count property value already equals the capacity, the capacity of the Dictionary<TKey,TValue> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

金鑰不能null,值可以是,如果但TValue是參考型別。A key cannot be null, but a value can be, if TValue is a reference type.

如果Count小於比容量,這個方法會接近 o (1) 作業。If Count is less than the capacity, this method approaches an O(1) operation. 如果必須增加容量,以容納新的項目,這個方法會變成 O (n) 運算,其中nCountIf the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

適用於

另請參閱