C# Hashtable
In C#, a Hashtable is a collection that stores data in the form of key-value pairs (
key=>value
), where keys and values can be any objects.
Each item in the hashtable has akey=>valuepair, where key is used to access the item in the collection.
A hashtable organizes and accesses data based on a hashing algorithm, providing efficient lookup, insertion, and deletion operations.
Hashtable is a non-generic collection located in the namespace System.Collections . If you need a generic version, you can use Dictionary。
Basic structure:
- Key(
Key): used to identify each data item; must be unique and cannot benull。 - Value(
Value): the data corresponding to the key, which can benull。
Characteristics:
- Key-Value Pair Storage: each key is unique and associated with a value.
- Hashing Algorithm: the storage location is located via the key's hash code; the average time complexity of lookup and insertion is O(1).
- Dynamic Capacity Adjustment: when the number of elements exceeds the hashtable's capacity, the table expands dynamically and storage is reallocated.
- Unordered Storage: the storage order of key-value pairs is independent of the insertion order and is determined by the hash code.
Methods and Properties of the Hashtable Class
The following table lists the Hashtable Some commonly used members of the class property:
| Attribute name | Type | Description |
|---|---|---|
Count |
int |
Gets the number of key-value pairs contained in the hashtable. |
IsReadOnly |
bool |
Indicates whether the hashtable is read-only. |
IsFixedSize |
bool |
Indicates whether the hashtable has a fixed size. |
IsSynchronized |
bool |
Indicates whether the hashtable is thread-safe. |
Keys |
ICollection |
Gets a collection of all keys in the hashtable. |
Values |
ICollection |
Gets a collection of all values in the hashtable. |
SyncRoot |
object |
Gets an object that can be used to synchronize access to the hashtable. |
The following table lists the Hashtable Some commonly used members of the class Method:
| Method Name | Return Type | Description |
|---|---|---|
| Adding and Removing | ||
Add(object key, object value) |
void |
Adds the specified key-value pair to the hashtable. |
Clear() |
void |
Clears the hashtable. |
Remove(object key) |
void |
Removes the key-value pair with the specified key. |
| Querying and Accessing | ||
Contains(object key) |
bool |
Determines whether the hashtable contains the specified key. |
ContainsKey(object key) |
bool |
Determines whether the hashtable contains the specified key (equivalent to Contains)。 |
ContainsValue(object value) |
bool |
Determines whether the hashtable contains the specified value. |
| Copying and Enumeration | ||
CopyTo(Array array, int index) |
void |
Copies the elements of the hashtable to an array, starting at the specified index. |
GetEnumerator() |
IDictionaryEnumerator |
Returns an enumerator for the hashtable, used to traverse the key-value pairs. |
Example
The following example demonstrates the concept of a Hashtable:
Example
using
System
;
using
System.Collections
;
namespace
CollectionsApplication
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
Hashtable ht
=
new
Hashtable
(
)
;
ht
.
Add
(
"001"
,
"Zara Ali"
)
;
ht
.
Add
(
"002"
,
"Abida Rehman"
)
;
ht
.
Add
(
"003"
,
"Joe Holzner"
)
;
ht
.
Add
(
"004"
,
"Mausam Benazir Nur"
)
;
ht
.
Add
(
"005"
,
"M. Amlan"
)
;
ht
.
Add
(
"006"
,
"M. Arif"
)
;
ht
.
Add
(
"007"
,
"Ritesh Saikia"
)
;
if
(
ht
.
ContainsValue
(
"Nuha Ali"
)
)
{
Console
.
WriteLine
(
"This student name is already in the list"
)
;
}
else
{
ht
.
Add
(
"008"
,
"Nuha Ali"
)
;
}
// Get the collection of keys
ICollection key
=
ht
.
Keys
;
foreach
(
string
k
in
key
)
{
Console
.
WriteLine
(
k
": "
ht
[
k
]
)
;
}
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
007: Ritesh Saikia
004: Mausam Benazir Nur
005: M. Amlan
008: Nuha Ali
002: Abida Rehman
003: Joe Holzner
001: Zara Ali
006: M. Arif
Comparison with Other Collections
| Feature | Hashtable |
Dictionary |
|---|---|---|
| Type Safety | No (non-generic, stores object type) |
Yes (generic, strongly typed constraints) |
| Performance | Slower (requires boxing and unboxing) | Faster (no boxing; generics support types directly) |
| Key Uniqueness | Is | Is |
Whether values can be null |
Is | Is |
| Sorting | None | None |
| Thread Safety | Not thread-safe by default | Not thread-safe by default |
Hashtableis an efficient key-value collection, suitable for scenarios that require fast storage and retrieval of key-value pairs.- Non-generic design makes it inferior in type safety and performance to generic collections (such as
Dictionary)。 - In modern C# development,
Dictionaryis more commonly used, butHashtableis still suitable for simple applications or scenarios that need compatibility with legacy code.
C# Collections