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>.

範例

注意

本文中的 C# 範例會在 Try.NET 內嵌程式碼執行器和測試區執行。The C# examples in this article run in the Try.NET inline code runner and playground. 選取 [執行] 按鈕以在互動式視窗中執行範例。Select the Run button to run an example in an interactive window. 執行程式碼之後,您便可以修改它,並再選取一次 [執行] 來執行修改過的程式碼。Once you execute the code, you can modify it and run the modified code by selecting Run again. 修改過的程式碼會在互動式視窗中執行,或是如果編譯失敗的話,互動式視窗會顯示所有 C# 編譯器錯誤訊息。The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

下列程式碼範例會建立具有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.

如果屬性值已經等於容量,則的容量Dictionary<TKey,TValue>會藉由自動重新配置內部陣列來增加,而且現有的專案會先複製到新的陣列,然後才新增新的元素。 CountIf 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.

適用於

另請參閱