IDictionary.Addとは? わかりやすく解説

IDictionary.Add メソッド

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

指定したキーおよび値を持つ要素を IDictionary に追加します。

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

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

instance.Add(key, value)
void Add (
    TKey key,
    TValue value
)
void Add (
    TKey key, 
    TValue value
)
void Add (
    TKey key, 
    TValue value
)
function Add (
    key : TKey, 
    value : TValue
)

パラメータ

key

追加する要素のキーとして使用するオブジェクト。

value

追加する要素の値として使用するオブジェクト。

例外例外
例外の種類条件

ArgumentNullException

key が null 参照 (Visual Basic では Nothing) です。

ArgumentException

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

NotSupportedException

IDictionary は読み取り専用です。

解説解説

Item プロパティを使用すると、ディクショナリ内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : C# の場合は myCollection["myNonexistentKey"] = myValue、Visual Basic の場合は myCollection("myNonexistentKey") = myValue) 。ただし、指定したキーがディクショナリに既に存在する場合、Item プロパティを設定すると、既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。

オブジェクトが等しいかどうかを判断する方法で実装が異なります。たとえば、List クラスでは Comparer.Default が使用されます。また、Dictionary クラスでは、IComparer 実装をキーの比較に使うかどうかをユーザーが指定できます。

key を null 参照 (Visual Basic では Nothing) にできるかどうかは、実装によって異なります。

使用例使用例

整数のキーを使用して文字列の空の Dictionary を作成し、IDictionary インターフェイスを介してアクセスするコード例を次に示します。このコード例では、Add メソッドを使用していくつかの要素を追加します。この例では、重複するキーを追加しようとすると、Add メソッドが ArgumentException をスローすることを示します。

このコードは、コンパイルして実行することのできる例の一部です。詳細については、System.Collections.Generic.IDictionary のトピックを参照してください。

' Create a new dictionary of strings, with string keys, 
' and access it through the IDictionary generic interface.
Dim openWith As IDictionary(Of
 String, String) = _
    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, 
// and access it through the IDictionary generic interface.
IDictionary<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.");
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
IDictionary ジェネリック インターフェイス
IDictionary メンバ
System.Collections.Generic 名前空間
Item
ICollection.IsReadOnly プロパティ

IDictionary.Add メソッド

指定したキーおよび値を持つ要素を IDictionary オブジェクトに追加します。

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

例外例外
例外の種類条件

ArgumentNullException

key が null 参照 (Visual Basic では Nothing) です。

ArgumentException

同じキーを持つ要素が、IDictionary オブジェクトに既に存在します。

NotSupportedException

IDictionary は読み取り専用です。

または

IDictionary が固定サイズです。

解説解説

Item プロパティを使用すると、ディクショナリ内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : myCollection["myNonexistentKey"] = myValue)。ただし、指定したキーがディクショナリに既に存在する場合、Item プロパティを設定すると、既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。

キーを null 参照 (Visual Basic では Nothing) にできるかどうかは、実装によって異なります。

使用例使用例

Add メソッドを実装する方法を次のコード例に示します。このコード例は、IDictionary クラスのトピックで取り上げているコード例の一部です。

Public Sub Add(ByVal key
 As Object, ByVal value
 As Object) Implements IDictionary.Add

    ' Add the new key/value pair even if this key already exists in
 the dictionary.
    If ItemsInUse = items.Length Then
        Throw New InvalidOperationException("The
 dictionary cannot hold any more items.")
    End If
    items(ItemsInUse) = New DictionaryEntry(key, value)
    ItemsInUse = ItemsInUse + 1
End Sub
public void Add(object key, object value) 
{
    // Add the new key/value pair even if this key already exists in
 the dictionary.
    if (ItemsInUse == items.Length)
        throw new InvalidOperationException("The dictionary
 cannot hold any more items.");
    items[ItemsInUse++] = new DictionaryEntry(key, value);
}
public:
    virtual void Add(Object^ key, Object^ value)
    {
        // Add the new key/value pair even if this key already exists
        // in the dictionary.
        if (itemsInUse == items->Length)
        {
            throw gcnew InvalidOperationException
                ("The dictionary cannot hold any more items.");
        }
        items[itemsInUse++] = gcnew DictionaryEntry(key, value);
    }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「IDictionary.Add」の関連用語

IDictionary.Addのお隣キーワード
検索ランキング

   

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



IDictionary.Addのページの著作権

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

©2026 GRAS Group, Inc.RSS