This property is used to get or set the value associated with the specified key in the Dictionary.
Syntax:
public TValue this[TKey key] { get; set; }
Here, key is the Key of the value to get or set.
Property Value: It is 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:
// 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 Dictionary named myDict Dictionary<string, string> myDict = new Dictionary<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); } } } |
Key = Australia, Value = Canberra Key = Belgium, Value = Brussels Key = Netherlands, Value = Amsterdam Key = China, Value = Beijing Key = Russia, Value = Moscow Key = India, Value = New Delhi 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 = Netherlands, Value = Amsterdam Key = China, Value = Beijing Key = Russia, Value = Saint Petersburg Key = India, Value = Mumbai
Reference:
Recommended Posts:
- StringBuilder.Chars[] Property in C#
- Thread.CurrentThread Property in C#
- C# | OrderedDictionary.Item[Object] Property
- C# | LongLength property of an Array
- C# | Dictionary.Values Property
- C# | Dictionary.Keys Property
- C# | Dictionary.Count Property
- C# | SortedDictionary.Item[] Property
- C# | SortedDictionary.Count Property
- C# | SortedDictionary.Keys Property
- C# | SortedDictionary.Values Property
- Queue.Count Property in C#
- Queue.IsSynchronized Property in C#
- Stack.Count Property in C#
- Stack.IsSynchronized Property in C#
- Console.KeyAvailable() Property in C#
- Console.TreatControlCAsInput Property in C# with examples
- How to use AcceptsReturn Property of TextBox in C#?
- How to use AutoCheck property of CheckBox in C#?
- Uri.IsLoopback Property in C# with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

