Adds an entry with the specified key and value into the ListDictionary.
[Visual Basic] Public Overridable Sub Add( _ ByVal key As Object, _ ByVal value As Object _ ) Implements IDictionary.Add [C#] public virtual void Add( object key, object value ); [C++] public: virtual void Add( Object* key, Object* value ); [JScript] public function Add( key : Object, value : Object );
| Exception Type | Condition |
|---|---|
| ArgumentNullException | key is a null reference (Nothing in Visual Basic). |
| ArgumentException | An entry with the same key already exists in the ListDictionary. |
An object that has no correlation between its state and its hash code value should typically not be used as the key. For example, String objects are better than StringBuilder objects for use as keys.
The Item property can also be used to add new elements by setting the value of a key that does not exist in the ListDictionary. For example: myCollection["myNonexistentKey"] = myValue. However, if the specified key already exists in the ListDictionary, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.
This is an O(n) operation, where n is Count.
[Visual Basic] ' The following code example adds to and removes elements from a ListDictionary. Imports System Imports System.Collections Imports System.Collections.Specialized Public Class SamplesListDictionary Public Shared Sub Main() ' Creates and initializes a new ListDictionary. Dim myCol As New ListDictionary() myCol.Add("Braeburn Apples", "1.49") myCol.Add("Fuji Apples", "1.29") myCol.Add("Gala Apples", "1.49") myCol.Add("Golden Delicious Apples", "1.29") myCol.Add("Granny Smith Apples", "0.89") myCol.Add("Red Delicious Apples", "0.99") ' Displays the values in the ListDictionary in three different ways. Console.WriteLine("Initial contents of the ListDictionary:") PrintKeysAndValues(myCol) ' Deletes a key. myCol.Remove("Plums") Console.WriteLine("The collection contains the following elements after removing ""Plums"":") PrintKeysAndValues(myCol) ' Clears the entire collection. myCol.Clear() Console.WriteLine("The collection contains the following elements after it is cleared:") PrintKeysAndValues(myCol) End Sub 'Main Public Shared Sub PrintKeysAndValues(myCol As IEnumerable) Dim myEnumerator As IEnumerator = myCol.GetEnumerator() Console.WriteLine(" KEY VALUE") Dim de As DictionaryEntry For Each de In myCol Console.WriteLine(" {0,-25} {1}", de.Key, de.Value) Next de Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesListDictionary 'This code produces the following output. ' 'Initial contents of the ListDictionary: ' KEY VALUE ' Braeburn Apples 1.49 ' Fuji Apples 1.29 ' Gala Apples 1.49 ' Golden Delicious Apples 1.29 ' Granny Smith Apples 0.89 ' Red Delicious Apples 0.99 ' 'The collection contains the following elements after removing "Plums": ' KEY VALUE ' Braeburn Apples 1.49 ' Fuji Apples 1.29 ' Gala Apples 1.49 ' Golden Delicious Apples 1.29 ' Granny Smith Apples 0.89 ' Red Delicious Apples 0.99 ' 'The collection contains the following elements after it is cleared: ' KEY VALUE ' [C#] // The following code example adds to and removes elements from a ListDictionary. using System; using System.Collections; using System.Collections.Specialized; public class SamplesListDictionary { public static void Main() { // Creates and initializes a new ListDictionary. ListDictionary myCol = new ListDictionary(); myCol.Add( "Braeburn Apples", "1.49" ); myCol.Add( "Fuji Apples", "1.29" ); myCol.Add( "Gala Apples", "1.49" ); myCol.Add( "Golden Delicious Apples", "1.29" ); myCol.Add( "Granny Smith Apples", "0.89" ); myCol.Add( "Red Delicious Apples", "0.99" ); // Displays the values in the ListDictionary in three different ways. Console.WriteLine( "Initial contents of the ListDictionary:" ); PrintKeysAndValues( myCol ); // Deletes a key. myCol.Remove( "Plums" ); Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); PrintKeysAndValues( myCol ); // Clears the entire collection. myCol.Clear(); Console.WriteLine( "The collection contains the following elements after it is cleared:" ); PrintKeysAndValues( myCol ); } public static void PrintKeysAndValues( IEnumerable myCol ) { IEnumerator myEnumerator = myCol.GetEnumerator(); Console.WriteLine( " KEY VALUE" ); foreach ( DictionaryEntry de in myCol ) Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); Console.WriteLine(); } } /* This code produces the following output. Initial contents of the ListDictionary: KEY VALUE Braeburn Apples 1.49 Fuji Apples 1.29 Gala Apples 1.49 Golden Delicious Apples 1.29 Granny Smith Apples 0.89 Red Delicious Apples 0.99 The collection contains the following elements after removing "Plums": KEY VALUE Braeburn Apples 1.49 Fuji Apples 1.29 Gala Apples 1.49 Golden Delicious Apples 1.29 Granny Smith Apples 0.89 Red Delicious Apples 0.99 The collection contains the following elements after it is cleared: KEY VALUE */ [C++] // The following code example adds to and removes elements from a ListDictionary. #using <mscorlib.dll> #using <System.dll> using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void PrintKeysAndValues(IEnumerable* myCol) { Console::WriteLine(S" KEY VALUE"); IEnumerator* myEnum = myCol->GetEnumerator(); while (myEnum->MoveNext()) { DictionaryEntry* de = __try_cast<DictionaryEntry*>(myEnum->Current); Console::WriteLine(S" {0, -25} {1}", de->Key, de->Value); } Console::WriteLine(); } int main() { // Creates and initializes a new ListDictionary. ListDictionary* myCol = new ListDictionary(); myCol->Add(S"Braeburn Apples", S"1.49"); myCol->Add(S"Fuji Apples", S"1.29"); myCol->Add(S"Gala Apples", S"1.49"); myCol->Add(S"Golden Delicious Apples", S"1.29"); myCol->Add(S"Granny Smith Apples", S"0.89"); myCol->Add(S"Red Delicious Apples", S"0.99"); // Displays the values in the ListDictionary in three different ways. Console::WriteLine(S"Initial contents of the ListDictionary:"); PrintKeysAndValues(myCol); // Deletes a key. myCol->Remove(S"Plums"); Console::WriteLine(S"The collection contains the following elements after removing \"Plums\":"); PrintKeysAndValues(myCol); // Clears the entire collection. myCol->Clear(); Console::WriteLine(S"The collection contains the following elements after it is cleared:"); PrintKeysAndValues(myCol); } /* This code produces the following output. Initial contents of the ListDictionary: KEY VALUE Braeburn Apples 1.49 Fuji Apples 1.29 Gala Apples 1.49 Golden Delicious Apples 1.29 Granny Smith Apples 0.89 Red Delicious Apples 0.99 The collection contains the following elements after removing S"Plums": KEY VALUE Braeburn Apples 1.49 Fuji Apples 1.29 Gala Apples 1.49 Golden Delicious Apples 1.29 Granny Smith Apples 0.89 Red Delicious Apples 0.99 The collection contains the following elements after it is cleared: KEY VALUE */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET
ListDictionary Class | ListDictionary Members | System.Collections.Specialized Namespace | Remove | Item | IDictionary.Add