StringDictionary.Add Method (String, String)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Adds an entry with the specified key and value into the StringDictionary.

Namespace:   System.Collections.Specialized
Assembly:  System (in System.dll)

public virtual void Add(
	string key,
	string value
)

Parameters

key
Type: System.String

The key of the entry to add.

value
Type: System.String

The value of the entry to add. The value can be null.

Exception Condition
ArgumentNullException

key is null.

ArgumentException

An entry with the same key already exists in the StringDictionary.

NotSupportedException

The StringDictionary is read-only.

The key is handled in a case-insensitive manner; it is translated to lowercase before it is added to the string dictionary.

This method is an O(1) operation.

The following code example demonstrates how to add and remove elements from a StringDictionary.

using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringDictionary  {

   public static void Main()  {

      // Creates and initializes a new StringDictionary.
      StringDictionary myCol = new StringDictionary();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );

      // Displays the values in the StringDictionary.
      Console.WriteLine( "Initial contents of the StringDictionary:" );
      PrintKeysAndValues( myCol );

      // Deletes an element.
      myCol.Remove( "green" );
      Console.WriteLine( "The collection contains the following elements after removing \"green\":" );
      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( StringDictionary myCol )  {
      Console.WriteLine( "   KEY        VALUE" );
      foreach ( DictionaryEntry de in myCol )
         Console.WriteLine( "   {0,-10} {1}", de.Key, de.Value );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringDictionary:
   KEY        VALUE
   green      verde
   red        rojo
   blue       azul

The collection contains the following elements after removing "green":
   KEY        VALUE
   red        rojo
   blue       azul

The collection contains the following elements after it is cleared:
   KEY        VALUE

*/

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
Show: