Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the Item property of Hashtable class in C#?
The Item property of the Hashtable class in C# gets or sets the value associated with the specified key. This property uses the indexer syntax [key] and allows you to both retrieve existing values and add new key-value pairs to the hashtable.
When a key does not exist in the hashtable, you can use the Item property to add it along with its value. This makes it convenient for both accessing and modifying hashtable contents.
Syntax
Following is the syntax for using the Item property −
// Getting a value object value = hashtable[key]; // Setting a value (adds if key doesn't exist) hashtable[key] = value;
Parameters
key − The key of the element to get or set. This can be any object type.
value − The value to associate with the specified key.
Return Value
Returns the value associated with the specified key, or null if the key is not found.
Using Item Property to Access and Modify Elements
The Item property can be used to retrieve existing values and add new key-value pairs −
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
// Adding elements using Add method
ht.Add("One", "Amit");
ht.Add("Two", "Aman");
ht.Add("Three", "Raman");
// Adding element using Item property
ht["Four"] = "David";
// Accessing elements using Item property
Console.WriteLine("Value for 'One': " + ht["One"]);
Console.WriteLine("Value for 'Four': " + ht["Four"]);
// Modifying existing element
ht["Two"] = "Updated Aman";
Console.WriteLine("Updated value for 'Two': " + ht["Two"]);
Console.WriteLine("\nAll elements:");
ICollection key = ht.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + ht[k]);
}
}
}
The output of the above code is −
Value for 'One': Amit Value for 'Four': David Updated value for 'Two': Updated Aman All elements: Three: Raman Four: David One: Amit Two: Updated Aman
Adding Non-Existent Keys
When you assign a value to a key that doesn't exist, the Item property automatically adds the new key-value pair −
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
// Adding elements to non-existent keys
ht["Name"] = "John";
ht["Age"] = 25;
ht["City"] = "New York";
Console.WriteLine("Elements added using Item property:");
foreach (DictionaryEntry entry in ht) {
Console.WriteLine(entry.Key + ": " + entry.Value);
}
// Checking if key exists before accessing
if (ht.ContainsKey("Country")) {
Console.WriteLine("Country: " + ht["Country"]);
} else {
Console.WriteLine("Country key not found");
ht["Country"] = "USA"; // Adding new key
Console.WriteLine("Added Country: " + ht["Country"]);
}
}
}
The output of the above code is −
Elements added using Item property: City: New York Age: 25 Name: John Country key not found Added Country: USA
Key Considerations
If you try to access a key that doesn't exist using the Item property, it returns
null.The Item property allows
nullvalues but notnullkeys.Unlike the
Addmethod, using the Item property with an existing key will overwrite the value instead of throwing an exception.
Conclusion
The Item property of the Hashtable class provides a convenient indexer syntax for getting and setting values by key. It automatically handles both accessing existing elements and adding new key-value pairs, making it more flexible than the Add method for certain scenarios.
