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. 参照型の場合は null の値を使用できます。The value can be null for reference types.

実装

例外

同じキーを持つ要素が、Dictionary<TKey,TValue> に既に存在します。An element with the same key already exists in the Dictionary<TKey,TValue>.

次のコード例は、TKey型としてstring、TValue型としてstringを持つ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) 操作、nCountします。If the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

適用対象

こちらもご覧ください