Class Hashtable
Initializes a new instance of the Hashtable class.
Inherited Members
Namespace: System.Collections
Assembly: nanoFramework.System.Collections.dll
Syntax
public class Hashtable : ICloneable, IDictionary, ICollection, IEnumerable
Remarks
The implementation for .NET nanoFramework, unlike the full .NET, doesn't support collisions so every key has to be truly unique through it's GetHashCode().
Constructors
Hashtable()
Initializes a new, empty instance of the Hashtable class using the default initial capacity, load factor, hash code provider and comparer.
Declaration
public Hashtable()
Remarks
A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. Capacity is automatically increased as required.
The load factor is the maximum ratio of elements to buckets. A smaller load factor means faster lookup at the cost of increased memory consumption.
When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.
The hash code provider dispenses hash codes for keys in the Hashtable object. The default hash code provider is the key's implementation of GetHashCode().
The .NET nanoFramework implementation uses the Equals(object) to perform comparisons of the key's.Hashtable(int, float)
Initializes a new, empty instance of the Hashtable class using the specified initial capacity and load factor, and the default hash code provider and comparer. load factor.
Declaration
public Hashtable(int capacity, float loadFactor)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The approximate number of elements that the Hashtable object can initially contain. |
float | loadFactor | A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets. |
Remarks
Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Capacity is automatically increased as required based on the load factor.
The load factor is the maximum ratio of elements to buckets. A smaller load factor means faster lookup at the cost of increased memory consumption.
When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.
The hash code provider dispenses hash codes for keys in the Hashtable object. The default hash code provider is the key's implementation of GetHashCode().
The .NET nanoFramework implementation uses the Equals(object) to perform comparisons of the key's.Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
-or>
-or>
|
ArgumentException |
|
Hashtable(int)
Initializes a new, empty instance of the Hashtable class using the specified initial capacity and load factor, and the default hash code provider and comparer.
Declaration
public Hashtable(int capacity)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The approximate number of elements that the Hashtable object can initially contain. |
Remarks
Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Capacity is automatically increased as required based on the load factor.
The load factor is the maximum ratio of elements to buckets. A smaller load factor means faster lookup at the cost of increased memory consumption.
When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.
The hash code provider dispenses hash codes for keys in the Hashtable object. The default hash code provider is the key's implementation of GetHashCode().
The .NET nanoFramework implementation uses the Equals(object) to perform comparisons of the key's.Exceptions
Type | Condition |
---|---|
ArgumentException |
|
Properties
Count
Gets the number of elements contained in the ICollection.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
int | The number of elements contained in the ICollection. |
IsFixedSize
Gets a value indicating whether the IDictionary object has a fixed size.
Declaration
public bool IsFixedSize { get; }
Property Value
Type | Description |
---|---|
bool | true if the IDictionary object has a fixed size; otherwise, false. |
IsReadOnly
Gets a value indicating whether the IDictionary object is read-only.
Declaration
public bool IsReadOnly { get; }
Property Value
Type | Description |
---|---|
bool | true if the IDictionary object is read-only; otherwise, false. |
IsSynchronized
Gets a value indicating whether access to the ICollection is synchronized (thread safe).
Declaration
public bool IsSynchronized { get; }
Property Value
Type | Description |
---|---|
bool | true if access to the ICollection is synchronized (thread safe); otherwise, false. |
this[object]
Gets or sets the element with the specified key.
Declaration
public object this[object key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
object | key | The key of the element to get or set. |
Property Value
Type | Description |
---|---|
object | The element with the specified key, or null if the key does not exist. |
Keys
Gets an ICollection object containing the keys of the IDictionary object.
Declaration
public ICollection Keys { get; }
Property Value
Type | Description |
---|---|
ICollection | An ICollection object containing the keys of the IDictionary object. |
SyncRoot
Gets an object that can be used to synchronize access to the ICollection.
Declaration
public object SyncRoot { get; }
Property Value
Type | Description |
---|---|
object | An object that can be used to synchronize access to the ICollection. |
Values
Gets an ICollection object containing the values in the IDictionary object.
Declaration
public ICollection Values { get; }
Property Value
Type | Description |
---|---|
ICollection | An ICollection object containing the values in the IDictionary object. |
Methods
Add(object, object)
Adds an element with the specified key and value into the Hashtable.
Declaration
public void Add(object key, object value)
Parameters
Type | Name | Description |
---|---|---|
object | key | The key of the element to add. |
object | value | The value of the element to add. The value can be null. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException | An element with the same key already exists in the Hashtable. |
Clear()
Removes all elements from the Hashtable.
Declaration
public void Clear()
Remarks
Count is set to zero, and references to other objects from elements of the collection are also released. The capacity remains unchanged.
Clone()
Creates a shallow copy of the Hashtable.
Declaration
public object Clone()
Returns
Type | Description |
---|---|
object | A shallow copy of the Hashtable. |
Remarks
A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new collection point to the same objects that the references in the original collection point to.
In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.
The Hashtable clone has the same count, the same capacity, the same hash provider, and the same comparer as the original Hashtable.
Contains(object)
Determines whether the Hashtable contains a specific key.
Declaration
public bool Contains(object key)
Parameters
Type | Name | Description |
---|---|---|
object | key | The key to locate in the Hashtable. |
Returns
Type | Description |
---|---|
bool | true if the Hashtable contains an element with the specified key; otherwise, false. |
Remarks
Contains(object) implements Contains(object).
This method uses the collection's objects' Equals(object) method on item to determine whether item exists.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
CopyTo(Array, int)
Declaration
public void CopyTo(Array array, int arrayIndex)
Parameters
Type | Name | Description |
---|---|---|
Array | array | The one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. The Array must have zero-based indexing. |
int | arrayIndex | The zero-based index in array at which copying begins. |
Remarks
The elements are copied to the Array in the same order in which the enumerator iterates through the Hashtable.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
ArgumentException | The number of elements in the source Hashtable is greater than the available space from |
GetEnumerator()
Returns an IEnumerator that iterates through the Hashtable.
Declaration
public IEnumerator GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator | An IEnumerator for the Hashtable. |
Remarks
The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators. Therefore, using foreach is recommended, instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. Reset() also brings the enumerator back to this position. At this position, Current is undefined. Therefore, you must call MoveNext() to advance the enumerator to the first element of the collection before reading the value of Current.
Current returns the same object until either MoveNext() or Reset() is called. MoveNext sets Current to the next element.
If MoveNext() passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext() returns false. When the enumerator is at this position, subsequent calls to MoveNext() also return false. If the last call to MoveNext() returned false, Current is undefined. To set Current to the first element of the collection again, you can call Reset() followed by MoveNext().
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Remove(object)
Removes the element with the specified key from the Hashtable.
Declaration
public void Remove(object key)
Parameters
Type | Name | Description |
---|---|---|
object | key | The key of the element to remove. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|