C# | Add key and value into StringDictionary - GeeksforGeeks

01 Feb, 2019
StringDictionary.Add(String, String) method is used to add an entry with the specified key and value into the StringDictionary.
Syntax:
public virtual void Add (string key, string value);

Parameters:
key: It is the key of the entry which is to be added.
value: It is the value of the entry which is to be added. The value can be null.
Exceptions:
ArgumentNullException : If the key is null.
ArgumentException : It is an entry with the same key already exists in the StringDictionary.
NotSupportedException : If the StringDictionary is read-only.
Below programs illustrate the use of StringDictionary.Add(String, String) method:
Example 1:
// C# code to add key and value// into the StringDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver code    public static void Main()    {          // Creating a StringDictionary named myDict        StringDictionary myDict = new StringDictionary();          // Adding key and value into the StringDictionary        myDict.Add("A", "Apple");        myDict.Add("B", "Banana");        myDict.Add("C", "Cat");        myDict.Add("D", "Dog");        myDict.Add("E", "Elephant");          // Displaying the keys and values in StringDictionary        foreach(DictionaryEntry dic in myDict)        {            Console.WriteLine(dic.Key + "  " + dic.Value);        }    }}
Output:
d  Dog
b  Banana
c  Cat
e  Elephant
a  Apple

Example 2:
// C# code to add key and value// into the StringDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver code    public static void Main()    {          // Creating a StringDictionary named myDict        StringDictionary myDict = new StringDictionary();          // Adding key and value into the StringDictionary        myDict.Add("A", "Apple");        myDict.Add("B", "Banana");        myDict.Add("C", "Cat");        myDict.Add("D", "Dog");          // It should raise "ArgumentException"        // as an entry with the same key        // already exists in the StringDictionary.        myDict.Add("C", "Code");          // Displaying the keys and values in StringDictionary        foreach(DictionaryEntry dic in myDict)        {            Console.WriteLine(dic.Key + "  " + dic.Value);        }    }}
Runtime Error:
Unhandled Exception:System.ArgumentException: Item has already been added. Key in dictionary: ‘c’ Key being added: ‘c’
Note:
The key is handled in a case-insensitive manner i.e, it is translated to lowercase before it is added to the string dictionary.
This method is an O(1) operation.
Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.add?view=netframework-4.7.2
CSharp-Collections-Namespace
CSharp-method
CSharp-Specialized-Namespace
CSharp-Specialized-StringDictionary
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
Convert String to Character Array in C#
C# | How to insert an element in an Array?
Linked List Implementation in C#