C# | Gets or sets the value at the specified key in StringDictionary Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 with the specified key. If the specified key is not found, Get returns null, and Set creates a new entry with the specified key. Exception: This property throws ArgumentNullException if the key is null. Below programs illustrate the use of above-discussed property: Example 1: CSharp // C# code to get or set the value at // the specified key in StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named my1 StringDictionary my1 = new StringDictionary(); // Adding key and value into the StringDictionary my1.Add("1", "C"); my1.Add("2", "C++"); my1.Add("3", "Java"); my1.Add("4", "Python"); my1.Add("5", "C#"); // Displaying the keys and // values in StringDictionary foreach(DictionaryEntry d in my1) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("\nAfter Item[String] Property: \n"); // setting the value at key 2 my1["2"] = "HTML"; // Displaying the keys and // values in StringDictionary foreach(DictionaryEntry d1 in my1) { Console.WriteLine(d1.Key + " " + d1.Value); } } } Output: 3 Java 5 C# 4 Python 2 C++ 1 C After Item[String] Property: 3 Java 4 Python 2 HTML 1 C 5 C# Example 2: CSharp // C# code to get or set the value at // the specified key in StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named my1 StringDictionary my1 = new StringDictionary(); // Adding key and value into the StringDictionary my1.Add("1", "HTML"); my1.Add("2", "CSS"); my1.Add("3", "PHP"); my1.Add("4", "MongoDB"); my1.Add("5", "AngularJS"); // Displaying the keys and // values in StringDictionary foreach(DictionaryEntry d in my1) { Console.WriteLine(d.Key + " " + d.Value); } Console.WriteLine("\nAfter Item[String] Property: \n"); // setting the value at Key 8 // here key 8 is not present // so it will add a new key/value // pair. see output my1["8"] = "C#"; // Displaying the keys and // values in StringDictionary foreach(DictionaryEntry d1 in my1) { Console.WriteLine(d1.Key + " " + d1.Value); } } } Output: 3 PHP 5 AngularJS 4 MongoDB 2 CSS 1 HTML After Item[String] Property: 3 PHP 4 MongoDB 2 CSS 1 HTML 8 C# 5 AngularJS Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.item?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get the number of key/value pairs in the StringDictionary K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace Similar Reads C# | Get or set the value associated with the specified key in StringDictionary StringDictionary is a specialized collection. It is found in the System.Collections.Specialized namespace. It only allows string keys and string values. It suffers from performance problems. It implements a hash table with the key and the value strongly typed to be strings rather than objects. Below 2 min read C# | Get the number of key/value pairs in the StringDictionary StringDictionary.Count property is used to get the number of key/value pairs in the StringDictionary. Syntax: public virtual int Count { get; } Return Value: It returns the number of key/value pairs in the StringDictionary. Note: Retrieving the value of this property is an O(1) operation. Below prog 2 min read C# | Gets or sets the element at the specified index in StringCollection StringCollection.Item[Int32] Property is used to get or set the element at the specified index. Syntax: public string this[int index] { get; set; } Here, index is the zero-based index of the entry to get or set. Return Value: It returns the element of String type at the specified index. Exception: T 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 C# | Remove entry with specified key from the StringDictionary StringDictionary.Remove(String) method is used to remove the entry with the specified key from the string dictionary. Syntax: public virtual void Remove (string key); Here, key is the key of the entry to remove. Exceptions: ArgumentNullException: If the key is null. NotSupportedException: If the Str 3 min read C# | Get or Set at specified index in StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. Properties: StringCollection accepts null as a valid value and allows duplicate elements. String co 2 min read Like