Dictionary<TKey, TValue>.ICollection<KeyValuePair<TKey, TValue>>.Add Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Adds the specified value to the ICollection<T> with the specified key.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- keyValuePair
- Type: System.Collections.Generic.KeyValuePair<TKey, TValue>
The KeyValuePair<TKey, TValue> structure representing the key and value to add to the Dictionary<TKey, TValue>.
Implements
ICollection<T>.Add(T)| Exception | Condition |
|---|---|
| ArgumentNullException | The key of keyValuePair is null. |
| ArgumentException | An element with the same key already exists in the Dictionary<TKey, TValue>. |
The following example shows how to use explicit interface implementations of the System.Collections.Generic.ICollection<T> generic interface to manipulate a Dictionary<TKey, TValue> object.
using System; using System.Collections.Generic; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Create a new dictionary of strings, with string keys, and // access it through the generic ICollection interface. The // generic ICollection interface views the dictionary as a // collection of KeyValuePair objects with the same type // arguments as the dictionary. // ICollection<KeyValuePair<String, String>> openWith = new Dictionary<String, String>(); // Add some elements to the dictionary. When elements are // added through the ICollection<T> interface, the keys // and values must be wrapped in KeyValuePair objects. // openWith.Add(new KeyValuePair<String, String>("txt", "notepad.exe")); openWith.Add(new KeyValuePair<String, String>("bmp", "paint.exe")); openWith.Add(new KeyValuePair<String, String>("dib", "paint.exe")); openWith.Add(new KeyValuePair<String, String>("rtf", "wordpad.exe")); outputBlock.Text += "\n"; foreach (KeyValuePair<string, string> element in openWith) { outputBlock.Text += String.Format("{0}, {1}", element.Key, element.Value) + "\n"; } // The Contains method also takes a KeyValuePair object. // outputBlock.Text += String.Format( "\nContains(KeyValuePair(\"txt\", \"notepad.exe\")): {0}", openWith.Contains(new KeyValuePair<String, String>("txt", "notepad.exe"))) + "\n"; // The Remove method takes a KeyValuePair object.) // // Use the Remove method to remove a key/value pair. outputBlock.Text += String.Format("\nRemove(new KeyValuePair(\"dib\", \"paint.exe\"))") + "\n"; openWith.Remove(new KeyValuePair<String, String>("dib", "paint.exe")); outputBlock.Text += "\n"; foreach (KeyValuePair<string, string> element in openWith) { outputBlock.Text += String.Format("{0}, {1}", element.Key, element.Value) + "\n"; } // Create an array of KeyValuePair objects and copy the // contents of the dictionary to it. // KeyValuePair<string, string>[] copy = new KeyValuePair<string, string>[openWith.Count]; openWith.CopyTo(copy, 0); // List the contents of the array. // outputBlock.Text += "\n"; foreach (KeyValuePair<string, string> element in copy) { outputBlock.Text += String.Format("{0}, {1}", element.Key, element.Value) + "\n"; } } } /* This code example produces the following output: txt, notepad.exe bmp, paint.exe dib, paint.exe rtf, wordpad.exe Contains(KeyValuePair("txt", "notepad.exe")): True Remove(new KeyValuePair("dib", "paint.exe")) txt, notepad.exe bmp, paint.exe rtf, wordpad.exe txt, notepad.exe bmp, paint.exe rtf, wordpad.exe */