Dictionary<TKey,TValue>.Add(TKey, TValue) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したキーと値をディクショナリに追加します。
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 できます。
実装
例外
key は nullです。
同じキーを持つ要素が既に 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 =
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.
let openWith = 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")
with :? ArgumentException ->
printfn "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[] プロパティを使用して、Dictionary<TKey,TValue>に存在しないキーの値を設定して新しい要素を追加することもできます。たとえば、myCollection[myKey] = myValue (Visual Basic では、myCollection(myKey) = myValue)。 ただし、指定したキーが既に Dictionary<TKey,TValue>に存在する場合は、 Item[] プロパティを設定すると、古い値が上書きされます。 これに対し、 Add メソッドは、指定したキーを持つ値が既に存在する場合に例外をスローします。
Count プロパティの値が既に容量と等しい場合は、内部配列を自動的に再割り当てすることでDictionary<TKey,TValue>の容量が増加し、既存の要素は新しい要素が追加される前に新しい配列にコピーされます。
キーを nullすることはできませんが、 TValue が参照型の場合は値を指定できます。
Count容量より小さい場合、このメソッドは O(1) 操作に近づきます。 新しい要素に対応するために容量を増やす必要がある場合、このメソッドは O(n) 操作になり、 n が Count。