Dictionary.Add メソッドとは? わかりやすく解説

Dictionary.Add メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub Add ( _
    key As TKey, _
    value As TValue _
)
Dim instance As Dictionary(Of
 TKey, TValue)
Dim key As TKey
Dim value As TValue

instance.Add(key, value)
public void Add (
    TKey key,
    TValue value
)
public:
virtual void Add (
    TKey key, 
    TValue value
) sealed
public final void Add (
    TKey key, 
    TValue value
)
public final function Add (
    key : TKey, 
    value : TValue
)

パラメータ

key

追加する要素キー

value

追加する要素の値。参照型場合は、値を null 参照 (Visual Basic では Nothing) にできます

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

ArgumentException

同じキー持つ要素が、Dictionary に既に存在します

解説解説

Item プロパティ使用すると、Dictionary 内に存在しないキーの値を設定することで、新し要素追加することもできます (例 : myCollection[myKey] = myValue) (Visual Basic では myCollection(myKey) = myValue)。ただし、指定したキーDictionary 内に既に存在する場合Item プロパティ設定する既存の値が上書きされます一方Add メソッドは、指定したキーを持つ値が既に存在する場合例外スローます。

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

キーnull 参照 (Visual Basic では Nothing) にすることはできませんが、TValue参照型である場合、値を null にすることはできます

Count容量より小さ場合、このメソッドは O(1) 操作近くなります新し要素格納するために容量増やす必要がある場合、このメソッドは O(n) 操作なります。ここで、nCount です。

使用例使用例

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

このコード例は、Dictionary クラストピック取り上げているコード例一部分です。

' 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
// 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.");
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からDictionary.Add メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDictionary.Add メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDictionary.Add メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Dictionary.Add メソッド」の関連用語

Dictionary.Add メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Dictionary.Add メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS