C# | Gets or sets the value in HybridDictionary with specified key
HybridDictionary.Item[Object] property is used to get or set the value associated with the specified key.
Syntax:
public object this[object key] { get; set; }
Here, key is the key whose value is to be get or set.
Return Value: The value associated with the specified key. If the specified key is not found, attempting to get it returns null, and attempting to set it creates a new entry using the specified key.
Exception: This property will give ArgumentNullException if the key is null.
Below programs illustrate the use of HybridDictionary.Item[Object] property:
Example 1:
// C# code to get or set the value // associated with the specified key // in HybridDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a HybridDictionary named myDict HybridDictionary myDict = new HybridDictionary(); // 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(DictionaryEntry de in myDict) { Console.WriteLine(de.Key + " " + de.Value); } // Displaying the value associated // with key "Russia" Console.WriteLine(myDict["Russia"]); // Setting the value associated with key "Russia" myDict["Russia"] = "Saint Petersburg"; // Displaying the value associated // with key "Russia" Console.WriteLine(myDict["Russia"]); // Displaying the value associated // with key "India" Console.WriteLine(myDict["India"]); // Setting the value associated with key "India" myDict["India"] = "Mumbai"; // Displaying the value associated // with key "India" Console.WriteLine(myDict["India"]); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) { Console.WriteLine(de.Key + " " + de.Value); } } } |
Output:
Australia Canberra Belgium Brussels Netherlands Amsterdam China Beijing Russia Moscow India New Delhi Moscow Saint Petersburg New Delhi Mumbai Australia Canberra Belgium Brussels Netherlands Amsterdam China Beijing Russia Saint Petersburg India Mumbai
Note:
- This property provides the ability to access a specific element in the collection by using the syntax : myCollection[key].
- A key cannot be null, but a value can.
- Retrieving the value of this property is an O(1) operation. Setting the property is also an O(1) operation.
Reference:
Recommended Posts:
- C# | HybridDictionary Class
- C# | Adding the specified key and value into HybridDictionary
- C# | Removing the specified key entry from HybridDictionary
- C# | Get an enumerator that iterates through the HybridDictionary
- C# | Get an ICollection containing the keys in HybridDictionary
- C# | Check if HybridDictionary is read only
- C# | Get an ICollection containing the values in HybridDictionary
- How to get Synchronize access to the HybridDictionary in C#
- C# | Removing all entries from HybridDictionary
- C# | Check the HybridDictionary for a specific key
- C# | Check if HybridDictionary has fixed size
- C# | Count the number of key/value pairs in HybridDictionary
- C# | Check if two HybridDictionary objects are equal
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Creating an empty HybridDictionary with specified case sensitivity
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.



