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

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

ArgumentException

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

NotSupportedException

IDictionary読み取り専用です。

解説解説

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

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

keynull 参照 (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

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



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

辞書ショートカット

すべての辞書の索引

「IDictionary.Add」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS