Dictionary<TKey,TValue>.Add(TKey, TValue) メソッド

定義

指定したキーと値をディクショナリに追加します。

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

追加する要素のキー。

value
TValue

追加する要素の値。 参照型の場合は null の値を使用できます。

実装

例外

keynullです。

同じキーを持つ要素が、Dictionary<TKey,TValue> に既に存在します。

次のコード例では、文字列キーを含む文字列の空 Dictionary<TKey,TValue> を作成し、メソッドを Add 使用していくつかの要素を追加します。 この例では、重複するキーを Add 追加しようとしたときにメソッドがスロー ArgumentException されることを示します。

このコード例は、Dictionary<TKey,TValue> クラスのために提供されている大規模な例の一部です。

// 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[]使用して、(Visual Basicでは) ;に存在しないキーの値をDictionary<TKey,TValue>設定することで、 myCollection[myKey] = myValue myCollection(myKey) = myValue新しい要素を追加することもできます。 ただし、指定したキーが既に Dictionary<TKey,TValue>存在する場合は、プロパティの設定によって Item[] 古い値が上書きされます。 これに対し、指定したキーを Add 持つ値が既に存在する場合、メソッドは例外をスローします。

プロパティ値が既に Count 容量と等しい場合は、内部配列を Dictionary<TKey,TValue> 自動的に再割り当てすることで、容量が増加し、新しい要素が追加される前に既存の要素が新しい配列にコピーされます。

キーを指定 nullすることはできませんが、参照型の場合 TValue は値を指定できます。

容量より小さい場合 Count 、このメソッドは O(1) 操作に近づきます。 新しい要素に対応するために容量を増やす必要がある場合、このメソッドは CountO(n) 操作になります。ここでn、 .

適用対象

こちらもご覧ください