C# | Add key and value into StringDictionary Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSHARP // C# code to add key and value // into the StringDictionary using 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: CSHARP // C# code to add key and value // into the StringDictionary using 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 Comment More infoAdvertise with us Next Article C# | Add key and value into StringDictionary S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace +1 More Similar Reads C# | Add key and value into OrderedDictionary collection OrderedDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the OrderedDictionary collection with the lowest available index. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the ent 2 min read C# | Add the specified key and value into the ListDictionary ListDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the ListDictionary. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the entry to add. The value can be null. Exceptions: Arg 2 min read C# | Get a collection of values in the StringDictionary StringDictionary.Values property is used to get a collection of values in the StringDictionary. Syntax: public virtual System.Collections.ICollection Values { get; } Return Value: An ICollection that provides the values in the StringDictionary. Example 1: CSHARP // C# code to get a collection // of 2 min read C# | Get a collection of keys in the StringDictionary StringDictionary.Keys property is used to get a collection of keys in the StringDictionary. Syntax: public virtual System.Collections.ICollection Keys { get; } Return Value: An ICollection that provides the keys in the StringDictionary. Below given are some examples to understand the implementation 2 min read C# | Check if the StringDictionary contains a specific value StringDictionary.ContainsValue(String) method is used to check whether the StringDictionary contains a specific value or not. Syntax: public virtual bool ContainsValue (string value); Here, value is the value to locate in the StringDictionary. The value can be null. Return Value: The method returns 2 min read How to create a StringDictionary in C# StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string k 2 min read C# | SortedDictionary.Values Property This property is used to get a collection containing the values in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.ValueCollection Values { get; } Return Value: It returns a collection containing the values in the SortedDictionary. Below are the pr 2 min read C# | Removing all entries from the StringDictionary StringDictionary.Clear method is used to remove all the entries from the StringDictionary. Syntax: public virtual void Clear (); Exception: This method will give the NotSupportedException if the StringDictionary is read-only. Example: CSHARP // C# code to remove all entries // from the StringDiction 2 min read C# | Gets or sets the value at the specified key in StringDictionary StringDictionary.Item[String] Property is used to get or set the value associated with the specified key. Syntax: public virtual string this[string key] { get; set; } Here, key is the Key of type System.String whose value is be to get or set. Return Value: This property returns the value associated 3 min read C# | Check if the StringDictionary contains a specific key StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not. Syntax: public virtual bool ContainsKey (string key); Here, key is the key to locate in the StringDictionary. Return Value: This method returns true if the StringDictionary conta 2 min read Like