Dictionary<TKey, TValue>.ICollection<KeyValuePair<TKey, TValue>>.Add Method
Adds the specified value to the ICollection<T> with the specified key.
Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Private Sub Add ( _
keyValuePair As KeyValuePair(Of TKey, TValue) _
) Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Add
'Usage
Dim instance As Dictionary
Dim keyValuePair As KeyValuePair(Of TKey, TValue)
CType(instance, ICollection(Of KeyValuePair(Of TKey, TValue))).Add(keyValuePair)
void ICollection<KeyValuePair<TKey, TValue>>.Add(
KeyValuePair<TKey, TValue> keyValuePair
)
private:
virtual void Add(
KeyValuePair<TKey, TValue> keyValuePair
) sealed = ICollection<KeyValuePair<TKey, TValue>>::Add
J# supports the use of explicit interface implementations, but not the declaration of new ones.
JScript does not support explicit interface implementations.
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
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | The key of keyValuePair is nulla null reference (Nothing in Visual Basic). |
ArgumentException | An element with the same key already exists in the Dictionary<TKey, TValue>. |
Remarks
If Count is less than the capacity, this method approaches an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
Examples
The following code example shows how to use the ICollection<KeyValuePair<TKey, TValue>>.Add, ICollection<KeyValuePair<TKey, TValue>>.Contains, ICollection<KeyValuePair<TKey, TValue>>.CopyTo, and ICollection<KeyValuePair<TKey, TValue>>.Remove methods of the System.Collections.Generic.ICollection<T> generic interface to manipulate a Dictionary<TKey, TValue> object.
Imports System
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' 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.
'
Dim openWith As ICollection(Of KeyValuePair(Of String, String)) _
= New Dictionary(Of String, String)
' Add some elements to the dictionary. When elements are
' added through the ICollection(Of T) interface, the keys
' and values must be wrapped in KeyValuePair objects.
'
openWith.Add(New KeyValuePair(Of String,String)("txt", "notepad.exe"))
openWith.Add(New KeyValuePair(Of String,String)("bmp", "paint.exe"))
openWith.Add(New KeyValuePair(Of String,String)("dib", "paint.exe"))
openWith.Add(New KeyValuePair(Of String,String)("rtf", "wordpad.exe"))
Console.WriteLine()
For Each element As KeyValuePair(Of String, String) in openWith
Console.WriteLine("{0}, {1}", element.Key, element.Value)
Next
' The Contains method also takes a KeyValuePair object.
'
Console.WriteLine(vbLf & _
"Contains(KeyValuePair(""txt"", ""notepad.exe"")): {0}", _
openWith.Contains(New KeyValuePair(Of String,String)("txt", "notepad.exe")))
' The Remove method takes a KeyValuePair object.)
'
' Use the Remove method to remove a key/value pair.
Console.WriteLine(vbLf & _
"Remove(New KeyValuePair(""dib"", ""paint.exe""))")
openWith.Remove(New KeyValuePair(Of String,String)("dib", "paint.exe"))
Console.WriteLine()
For Each element As KeyValuePair(Of String, String) in openWith
Console.WriteLine("{0}, {1}", element.Key, element.Value)
Next
' Create an array of KeyValuePair objects and copy the
' contents of the dictionary to it. Subtract one from the
' array size because Visual Basic allocates an extra array
' element.
Dim copy(openWith.Count - 1) As KeyValuePair(Of String, String)
openWith.CopyTo(copy, 0)
' List the contents of the array.
'
Console.WriteLine()
For Each element As KeyValuePair(Of String, String) in copy
Console.WriteLine("{0}, {1}", element.Key, element.Value)
Next
End Sub
End Class
' 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
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// 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"));
Console.WriteLine();
foreach( KeyValuePair<string, string> element in openWith )
{
Console.WriteLine("{0}, {1}", element.Key, element.Value);
}
// The Contains method also takes a KeyValuePair object.
//
Console.WriteLine(
"\nContains(KeyValuePair(\"txt\", \"notepad.exe\")): {0}",
openWith.Contains(new KeyValuePair<String,String>("txt", "notepad.exe")));
// The Remove method takes a KeyValuePair object.)
//
// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(new KeyValuePair(\"dib\", \"paint.exe\"))");
openWith.Remove(new KeyValuePair<String,String>("dib", "paint.exe"));
Console.WriteLine();
foreach( KeyValuePair<string, string> element in openWith )
{
Console.WriteLine("{0}, {1}", element.Key, element.Value);
}
// 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.
//
Console.WriteLine();
foreach( KeyValuePair<string, string> element in copy )
{
Console.WriteLine("{0}, {1}", element.Key, element.Value);
}
}
}
/* 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
*/
Platforms
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information
.NET Framework
Supported in: 3.5, 3.0 SP1, 3.0, 2.0 SP1, 2.0
.NET Compact Framework
Supported in: 3.5, 2.0
XNA Framework
Supported in: 1.0
See Also
Reference
Dictionary<TKey, TValue> Class