C# | Get or Set the value associated with specified key in Hashtable
Hashtable.Item[Object] Property is used to get or set the value associated with the specified key in the Hashtable.
Syntax:
public virtual object this[object key] { get; set; }
Here, key is key of object type whose value is to get or set.
Exceptions:
- ArgumentNullException: If the key is null.
- NotSupportedException: If the property is set and the Hashtable is read-only. Or the property is set, key does not exist in the collection, and the Hashtable has a fixed size.
Note:
- This property returns the value associated with the specific key. If that key is not found, and one is trying to get that, then this property will return null and if trying to set, it will result into the creation of a new element with the specified key.
- Retrieving and setting the value of this property is an O(1) operation.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# code to Gets or sets the value // associated with the specified key using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("g", "geeks"); myTable.Add("c", "c++"); myTable.Add("d", "data structures"); myTable.Add("q", "quiz"); // Get a collection of the keys. ICollection c = myTable.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); // Setting the value associated with key "c" myTable["c"] = "C#"; Console.WriteLine("Updated Values:"); // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); } } |
chevron_right
filter_none
Output:
d: data structures c: c++ q: quiz g: geeks Updated Values: d: data structures c: C# q: quiz g: geeks
Example 2:
// C# code to Gets or sets the value // associated with the specified key using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("4", "Even"); myTable.Add("9", "Odd"); myTable.Add("5", "Odd and Prime"); myTable.Add("2", "Even and Prime"); // Get a collection of the keys. ICollection c = myTable.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); // Setting the value associated // with key "56" which is not present // will result in the creation of // new key and value will be set which // is given by the user myTable["56"] = "New Value"; Console.WriteLine("Updated Values:"); // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); } } |
chevron_right
filter_none
Output:
5: Odd and Prime 9: Odd 2: Even and Prime 4: Even Updated Values: 5: Odd and Prime 9: Odd 2: Even and Prime 56: New Value 4: Even
Reference:
Recommended Posts:
- C# | Check if a Hashtable is equal to another Hashtable
- C# | Hashtable Class
- C# Hashtable with Examples
- C# | Check if the Hashtable contains a specific Value
- C# | Remove all elements from the Hashtable
- C# | Remove the element with the specified key from the Hashtable
- C# | Gets an ICollection containing the keys in the Hashtable
- Difference between Hashtable and Dictionary in C#
- C# | Check whether a Hashtable contains a specific key or not
- C# | How to get hash code for the specified key of a Hashtable
- C# | Check if Hashtable is read-only
- C# | Get an enumerator that iterates through the Hashtable
- C# | Gets an ICollection containing the values in the Hashtable
- C# | Check if the Hashtable contains a specific Key
- C# | Adding an element into the Hashtable
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.


