![]() |
VOOZH | about |
Hashtable class in C# is a part of the operations. It represents a collection of key-value pairs that are organized based on the hash code of the key. The Hashtable class provides various types of methods that are used to perform different types of operations on the hashtables. In Hashtable, keys are used to access the elements present in the collection. For very large Hashtable objects, we can increase the maximum capacity to 2 billion elements on a 64-bit system.
Example: This example demonstrates how to create a Hashtable, add key-value pairs, and display the elements.
Hashtable Elements: Two: 2 Three: 3 One: 1
In C#, the Hashtable is declared as:
Hashtable ht = new Hashtable();
| Constructor | Description |
|---|---|
| Hashtable() | Initalizes a new, empty instance with default parameters (initial capacity, load factor, etc.). |
| Hashtable(IDictionary) | Copies elements from the specified dictionary to a new Hashtable with default parameters |
| Hashtable(IDictionary, IEqualityComparer) | Copies elements from the specified dictionary to a new Hashtable and uses the provided IEqualityComparer |
| Hashtable(Int32) | Initializes a new, empty instance with a specified initial capacity and default parameters. |
| Hashtable(Int32, Single) | Initializes a new, empty instance with a specified initial capacity and load factor. |
| Hashtable(Int32, Single, IEqualityComparer) | Initializes a new, empty instance with a specified initial capacity, load factor, and IEqualityComparer |
| Hashtable(SerializationInfo, StreamingContext) | Initalizes a new instance from serialized data (for deserialization purposes). |
Example: This example demonstrates how to create and initialize two Hashtable objects, insert key-value pairs and print the mappings.
Mappings of ht1: 3 - three 2 - two 1 - one Mappings of ht2: 6 - six 5 - five 4 - four
| Properties | Description |
|---|---|
| comparer | Gets or sets the IComparer to use for the Hashtable. |
| Count | Gets the number of key/value pairs contained in the Hashtable. |
| EqualityComparer | Gets the IEqualityComparer to use for the Hashtable. |
| hcp | Gets or sets the object that can dispense hash codes. |
| IsFixedSize | Gets a value indicating whether the Hashtable has a fixed size. |
| IsReadOnly | Gets a value indicating whether the Hashtable is read-only. |
| IsSynchronized | Gets a value indicating whether access to the Hashtable is synchronized (thread safe). |
| Item[Object] | Gets or sets the value associated with the specified key. |
| Keys | Gets an ICollection containing the keys in the Hashtable. |
| SyncRoot | Gets an object that can be used to synchronize access to the Hashtable. |
| Values | Gets an ICollection containing the values in the Hashtable. |
Example: This example demonstrates how to use the IsSynchronized property check if a Hashtable is thread-safe and the Count property to get the number of elements in a Hashtable.
h1 Hashtable is not synchronized. h2 Hashtable is synchronized. Total Number of Elements in h1: 5
| Method | Description |
|---|---|
| Add(Object, Object) | Adds an element with the specified key and value into the Hashtable. |
| Clear() | Removes all elements from the Hashtable. |
| Clone() | Creates a shallow copy of the Hashtable. |
| Contains(Object) | Determines whether the Hashtable contains a specific key. |
| ContainsKey(Object) | Determines whether the Hashtable contains a specific key. |
| ContainsValue(Object) | Determines whether the Hashtable contains a specific value. |
| CopyTo(Array, Int32) | Copies the Hashtable elements to a one-dimensional Array instance at the specified index. |
| Equals(Object) | Determines whether the specified object is equal to the current object. |
| GetEnumerator() | Returns an IDictionaryEnumerator that iterates through the Hashtable. |
| GetHashCode() | Serves as the default hash function. |
| GetObjectData(SerializationInfo, StreamingContext) | Implements the ISerializable interface and returns the data needed to serialize the Hashtable. |
| GetType() | Gets the Type of the current instance. |
| Remove(Object) | Removes the element with the specified key from the Hashtable. |
| Synchronized(Hashtable) | Returns a synchronized (thread-safe) wrapper for the Hashtable. |
| ToString() | Returns a string that represents the current object. |
Example:
Hashtable Contains: 3-Geek3 5-Geek5 4-Geek4 2-Geek2 1-Geek1 Hashtable after removing element 4: 3-Geek3 2-Geek2 1-Geek1