Class Hashtable
- Namespace
- System.Collections
- Assembly
- nanoFramework.System.Collections.dll
Initializes a new instance of the Hashtable class.
public class Hashtable : ICloneable, IDictionary, ICollection, IEnumerable
- Inheritance
-
Hashtable
- Implements
- Inherited Members
- Extension Methods
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.
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)
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.
public Hashtable(int capacity)
Parameters
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
- ArgumentException
capacity
is causing an overflow.
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.
public Hashtable(int capacity, float loadFactor)
Parameters
capacity
intThe approximate number of elements that the Hashtable object can initially contain.
loadFactor
floatA 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
- ArgumentOutOfRangeException
capacity
is less than zero.-or>
loadFactor
is less than 0.1.-or>
loadFactor
is greater than 1.0.- ArgumentException
capacity
is causing an overflow.
Properties
Count
Gets the number of elements contained in the ICollection.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection.
IsFixedSize
Gets a value indicating whether the IDictionary object has a fixed size.
public bool IsFixedSize { get; }
Property Value
- bool
true if the IDictionary object has a fixed size; otherwise, false.
IsReadOnly
Gets a value indicating whether the IDictionary object is read-only.
public bool IsReadOnly { get; }
Property Value
- 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).
public bool IsSynchronized { get; }
Property Value
- bool
true if access to the ICollection is synchronized (thread safe); otherwise, false.
this[object]
Gets or sets the element with the specified key.
public object this[object key] { get; set; }
Parameters
key
objectThe key of the element to get or set.
Property Value
Keys
Gets an ICollection object containing the keys of the IDictionary object.
public ICollection Keys { get; }
Property Value
- 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.
public object SyncRoot { get; }
Property Value
- 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.
public ICollection Values { get; }
Property Value
- 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.
public void Add(object key, object value)
Parameters
key
objectThe key of the element to add.
value
objectThe value of the element to add. The value can be null.
Exceptions
- ArgumentNullException
key
is null.- ArgumentException
An element with the same key already exists in the Hashtable.
Clear()
Removes all elements from the Hashtable.
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.
public object Clone()
Returns
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.
public bool Contains(object key)
Parameters
Returns
Remarks
Contains(object) implements Contains(object).
This method uses the collection's objects' Equals(object) method on item to determine whether item exists.
Exceptions
- ArgumentNullException
key
is null.
CopyTo(Array, int)
public void CopyTo(Array array, int arrayIndex)
Parameters
array
ArrayThe one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. The Array must have zero-based indexing.
arrayIndex
intThe 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
- ArgumentNullException
array
is null.- ArgumentOutOfRangeException
arrayIndex
is less than zero.- ArgumentException
The number of elements in the source Hashtable is greater than the available space from
arrayIndex
to the end of the destination array.
GetEnumerator()
Returns an IEnumerator that iterates through the Hashtable.
public IEnumerator GetEnumerator()
Returns
- 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.
public void Remove(object key)
Parameters
key
objectThe key of the element to remove.
Exceptions
- ArgumentNullException
key
is null.