IDictionary.Add メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

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


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."); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


IDictionary.Add メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



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); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- IDictionary.Addのページへのリンク