C# | Get or Set the value associated with specified key in Hashtable Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: CSharp // 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]); } } Output: d: data structures c: c++ q: quiz g: geeks Updated Values: d: data structures c: C# q: quiz g: geeks Example 2: CSharp // 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]); } } 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: https://learn.microsoft.com/en-us/dotnet/api/system.collections.hashtable.item?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get or set the value associated with the specified key in StringDictionary K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Collections-Hashtable CSharp-Collections-Namespace Similar Reads C# | Get or set the value associated with specified key in ListDictionary ListDictionary.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 to get or set. Return Value : The value associated with the specified key. If the specified key is not found, 2 min read 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# | How to get hash code for the specified key of a Hashtable Hashtable.GetHash(Object) method is used to get the hashcode of the specified key of a Hashtable object. This method is inherited from the Object Class. Syntax: protected virtual int GetHash(Object Key); Exception: This method will give NullReferenceException if the key is null. Below programs illus 2 min read C# | Remove the element with the specified key from the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Remove(Object) Method is used to remove the element with the specified key from the Hashtable. Syntax: public virt 2 min read C# | Count the number of key/value pairs in the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable. Syntax: myTable. 2 min read C# | Check if the Hashtable contains a specific Value The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: publ 2 min read Like