C# | SortedDictionary.Item[] Property Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report This property is used to get or set the value associated with the specified key. Syntax: public TValue this[TKey key] { get; set; } Here, key is the Key of the value to get or set. Property Value: The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key. Exceptions: ArgumentNullException: If the key is null. KeyNotFoundException: If the property is retrieved and key does not exist in the collection. Example: csharp // C# code to get or set the value // associated with the specified key using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedDictionary named myDict SortedDictionary<string, string> myDict = new SortedDictionary<string, string>(); // Adding key/value pairs in myDict myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Displaying the key/value pairs in myDict foreach(KeyValuePair<string, string> k in myDict) { Console.WriteLine("Key = {0}, Value = {1}", k.Key, k.Value); } // Displaying the value associated // with key "Russia" Console.Write("\nValue associated with Russia: "); Console.Write(myDict["Russia"]); // Setting the value associated with key "Russia" myDict["Russia"] = "Saint Petersburg"; // Displaying the value associated // with key "Russia" Console.Write("\n\nValue associated with"+ " Russia After Setting: "); Console.Write(myDict["Russia"]); // Displaying the value associated // with key "India" Console.Write("\n\nValue associated with India: "); Console.Write(myDict["India"]); // Setting the value associated with key "India" myDict["India"] = "Mumbai"; // Displaying the value associated // with key "India" Console.Write("\n\nValue associated "+ "with India After Setting: "); Console.Write(myDict["India"]); Console.WriteLine("\n"); // Displaying the key/value pairs in myDict foreach(KeyValuePair<string, string> k1 in myDict) { Console.WriteLine("Key = {0}, Value = {1}", k1.Key, k1.Value); } } } Output: Key = Australia, Value = Canberra Key = Belgium, Value = Brussels Key = China, Value = Beijing Key = India, Value = New Delhi Key = Netherlands, Value = Amsterdam Key = Russia, Value = Moscow Value associated with Russia: Moscow Value associated with Russia After Setting: Saint Petersburg Value associated with India: New Delhi Value associated with India After Setting: Mumbai Key = Australia, Value = Canberra Key = Belgium, Value = Brussels Key = China, Value = Beijing Key = India, Value = Mumbai Key = Netherlands, Value = Amsterdam Key = Russia, Value = Saint Petersburg Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.sorteddictionary-2.item?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | SortedDictionary.Item[] Property K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Generic-Namespace CSharp SortedDictionary Class Similar Reads C# | SortedDictionary.Keys Property This property is used to get a collection containing the keys in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the SortedDictionary. Below are the programs 2 min read C# | SortedDictionary.Count Property This property is used to get the number of key/value pairs contained in the SortedDictionary<TKey, TValue>. Syntax: public int Count { get; } Return Value : It returns the number of key/value pairs contained in the SortedDictionary. Below are the programs to illustrate the use of above-discuss 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# | SortedDictionary.Remove() Method This method is used to remove the value with the specified key from the SortedDictionary<TKey, TValue>. Syntax: public bool Remove (TKey key); Return Value: This method returns true if the element is successfully found and removed; otherwise it returns false. This method returns false if key i 2 min read C# | SortedDictionary.Clear() Method This method is used to remove all key/value pairs from the SortedDictionary<TKey, TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of the above-discussed method: Example 1: csharp // C# code to remove all pairs // from SortedDictionary using System; using Sys 2 min read Like