public class Hashtable : ICloneable, ICollection, IDictionary, IEnumerable
Object
HashtableThis type implements IDictionary, ICollection, IEnumerable, and ICloneable.
mscorlib
BCL
Represents a hash table.
A Hashtable represents a dictionary with a constant lookup time that contains entries of associated keys and values. The type of each entry in a Hashtable is DictionaryEntry. A statement that exposes each element in the collection is required to iterate over this type. [Note: See example.]
Objects used as keys in a Hashtable are required to either implement both System.Object.GetHashCode and System.Object.Equals(System.Object) or neither. Furthermore, for a particular key, these methods are required to produce the same results when called with the same parameters while that key exists in a particular Hashtable . Keys cannot be mutated while they are used in the table.
Every key in a Hashtable is required to be unique compared to every other key in the table. An object that implements IComparer can determine whether two keys are unequal. The default comparer for a key is the key's implementation of System.Object.Equals(System.Object).
Each value in a Hashtable is required to provide its own hash function, which can be accessed by calling System.Collections.Hashtable.GetHash(System.Object). Alternatively, if an object that implements IHashCodeProvider is passed to a Hashtable constructor, the custom hash function provided by that object is used for every value in the table.
[Note: The default capacity (i.e. the default number of entries that can be contained) of a Hashtable is zero.
When an entry is added to the Hashtable, the entry is placed into a bucket based on the hash code obtained from the IHashCodeProvider implementation of the table, or the System.Object.GetHashCode if no specific IHashCodeProvider was provided. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, substantially reducing the number of key comparisons required to find an entry.
As entries are added to a Hashtable, and the maximum capacity of the table is reached, the number of buckets in the table is automatically increased to the smallest prime number that is larger than twice the current number of buckets.
A Hashtable can safely support one writer and multiple readers concurrently. To support multiple writers, all operations are required to be done through the wrapper returned by the System.Collections.Hashtable.Synchronized(System.Collections.Hashtable) method.
]
The following example shows how to iterate over the elements of a Hashtable.
[C#]
foreach (DictionaryEntry myEntry in myHashtable)
DefaultMemberAttribute("Item")
System.Collections Namespace
Hashtable Constructors
Hashtable(System.Collections.IDictionary, System.Collections.IHashCodeProvider, System.Collections.IComparer) Constructor
Hashtable(System.Collections.IDictionary) Constructor
Hashtable(int, System.Collections.IHashCodeProvider, System.Collections.IComparer) Constructor
Hashtable(System.Collections.IHashCodeProvider, System.Collections.IComparer) Constructor
Hashtable(int) Constructor
Hashtable() Constructor
Hashtable Methods
Hashtable.Add Method
Hashtable.Clear Method
Hashtable.Clone Method
Hashtable.Contains Method
Hashtable.ContainsKey Method
Hashtable.ContainsValue Method
Hashtable.CopyTo Method
Hashtable.GetEnumerator Method
Hashtable.GetHash Method
Hashtable.KeyEquals Method
Hashtable.Remove Method
Hashtable.Synchronized Method
Hashtable.System.Collections.IEnumerable.GetEnumerator Method
Hashtable Properties
Hashtable.Count() Property
Hashtable.Count() Property
Hashtable.IsFixedSize() Property
Hashtable.IsFixedSize() Property
Hashtable.IsReadOnly() Property
Hashtable.IsReadOnly() Property
Hashtable.IsSynchronized() Property
Hashtable.IsSynchronized() Property
Hashtable.Item Property
Hashtable.Keys() Property
Hashtable.Keys() Property
Hashtable.SyncRoot() Property
Hashtable.SyncRoot() Property
Hashtable.Values() Property
Hashtable.Values() Property
public Hashtable(IDictionary d, IHashCodeProvider hcp, IComparer comparer);
Constructs and initializes a new instance of the Hashtable class using the values of the specified IDictionary, the specified IHashCodeProvider, and the specified IComparer.
- d
- The IDictionary used to initialize the elements of the new instance.
- hcp
- The IHashCodeProvider that supplies the hash codes for all keys in the new instance; or,
null
to use the default hash code provider.
- comparer
- The IComparer to use to determine whether two keys are equal in the new instance, or
null
to use the default comparer.
Exception Type Condition ArgumentNullException d is null
.
The initial capacity of the new instance is set to the number of entries in d.
System.Collections.Hashtable Class, System.Collections Namespace
public Hashtable(IDictionary d);
Constructs and initializes a new instance of the Hashtable class using the values of the specified IDictionary.
- d
- The IDictionary used to initialize the elements of the new instance.
Exception Type Condition ArgumentNullException d is null
.
The initial capacity of the new instance is set to the number of entries in d. The new instance is initialized with the default IHashCodeProvider and IComparer.
System.Collections.Hashtable Class, System.Collections Namespace
public Hashtable(int capacity, IHashCodeProvider hcp, IComparer comparer);
Constructs and initializes a new instance of the Hashtable class with the specified initial capacity, the specified IHashCodeProvider, and the specified IComparer.
- capacity
- A Int32 that specifies the minimum number of entries that the new Hashtable instance can initially contain.
- hcp
- The IHashCodeProvider that supplies the hash codes for all keys in the Hashtable; or,
null
to use the default hash code provider.
- comparer
- The IComparer to use to determine whether two keys are equal, or
null
to use the default comparer.
System.Collections.Hashtable Class, System.Collections Namespace
public Hashtable(IHashCodeProvider hcp, IComparer comparer);
Constructs and initializes a new instance of the Hashtable class with the specified IHashCodeProvider and the specified IComparer.
- hcp
- The IHashCodeProvider that supplies the hash codes for all keys in the Hashtable; or,
null
to use the default hash code provider.
- comparer
- The IComparer to use to determine whether two keys are equal; or,
null
to use the default comparer.
The new instance is initialized with the default capacity.
System.Collections.Hashtable Class, System.Collections Namespace
public Hashtable(int capacity);
Constructs and initializes a new instance of the Hashtable class with the specified initial capacity.
- capacity
- A Int32 that specifies the minimum number of entries that the new Hashtable instance can initially contain.
Exception Type Condition ArgumentOutOfRangeException capacity < 0.
The new instance is initialized with the default IHashCodeProvider and IComparer.The number of entries that the new instance can contain can be greater than capacity.
System.Collections.Hashtable Class, System.Collections Namespace
public Hashtable();
Constructs and initializes a new instance of the Hashtable class.
The new instance is initialized with the default capacity, IHashCodeProvider, and IComparer.
System.Collections.Hashtable Class, System.Collections Namespace
public virtual void Add(object key, object value);
Adds an entry with the specified key and value into the current instance.
- key
- The key of the entry to add.
- value
- The value of the entry to add.
Exception Type Condition ArgumentNullException key is null
.
ArgumentException An entry with the same key already exists in the current instance.
NotSupportedException The current instance is read-only or has a fixed size.
System.Collections.Hashtable Class, System.Collections Namespace
public virtual void Clear();
Removes all entries from the current instance.
Exception Type Condition NotSupportedException The current instance is read-only.
[Note: This method is implemented to support the IDictionary interface.]
[Behaviors: As described above.]
[Default: The value of each key and value in the current instance is set to
null
. The System.Collections.Hashtable.Count property of the current instance is set to zero. The capacity of the current instance remains unchanged.If the current instance is empty, it remains unchanged and no exception is thrown.
]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual object Clone();
Creates a Object that is a copy of the current instance.
A Object that is a copy of the current instance.
[Note: This method is implemented to support the ICloneable interface.]
[Behaviors: As described above.]
[Default: This method creates a new Hashtable instance is initialized with the same count, IHashCodeProvider implementation, and IComparer implementation as the current instance. The references to the objects contained by the current instance are copied to the new instance.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual bool Contains(object key);
Determines whether the current instance contains the specified key.
- key
- The key to locate in the current instance.
true
if the current instance contains key; otherwise,false
.
Exception Type Condition ArgumentNullException key is null
.
[Note: This method is implemented to support the IDictionary interface.]
[Behaviors: As described above.]
[Default: This method is equivalent to System.Collections.Hashtable.ContainsKey(System.Object).]
[Note: For the default implementation, this method has a constant (O(1)) lookup time.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual bool ContainsKey(object key);
Determines whether the current instance contains an entry with the specified key.
- key
- The key of the entry to locate in the current instance.
true
if the current instance contains an entry with key; otherwise,false
.
Exception Type Condition ArgumentNullException key is null
.
[Behaviors: As described above.]
[Default: This method uses System.Collections.Hashtable.KeyEquals(System.Object,System.Object) to compare key to the keys in the current instance. ]
[Note: For the default implementation, this method has a constant (O(1)) lookup time.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual bool ContainsValue(object value);
Determines whether the current instance contains an entry with the specified value.
- value
- The value to locate in the current instance.
true
if the current instance contains an entry with value; otherwise,false
.
[Note: This method is implemented to support the IDictionary interface.]
[Behaviors: As described above.]
[Default: This method is equivalent to System.Collections.Hashtable.ContainsKey(System.Object).]
[Note: For the default implementation, this method has a constant (O(1)) lookup time.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual void CopyTo(Array array, int arrayIndex);
Copies the entries of the current instance to a one-dimensional Array starting at the specified index.
- array
- The one-dimensional, zero-indexed Array that is the destination of the objects copied from the current instance.
- arrayIndex
- A Int32 that specifies the zero-based index in array at which copying begins. This value is between 0 and array.Length minus the System.Collections.Hashtable.Count of the current instance, inclusive.
Exception Type Condition ArgumentNullException array is null
.ArgumentOutOfRangeException arrayIndex < 0. ArgumentException array has more than one dimension. -or-
arrayIndex > array.Length - The System.Collections.Hashtable.Count of the current instance.
InvalidCastException The type of the current instance is not assignment-compatible with the type of array.
[Behaviors: As described above.]
[Default: The DictionaryEntry elements in the current instance are copied to the Array in the same order in which they are contained the current instance. If DictionaryEntry is not assignment-compatible with the type of array, a InvalidCastException is thrown. If an exception is thrown while copying, the state of the current instance is undefined.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual IDictionaryEnumerator GetEnumerator();
Returns a IDictionaryEnumerator for the current instance.
A IDictionaryEnumerator for the current instance.
If the current instance is modified while an enumeration is in progress, a call to System.Collections.IEnumerator.MoveNext or System.Collections.IEnumerator.Reset throws InvalidOperationException.[Note: For detailed information regarding the use of an enumerator, see IEnumerator.
This property is implemented to support the IList interface.
]
[Behaviors: As described above.]
System.Collections.Hashtable Class, System.Collections Namespace
protected virtual int GetHash(object key);
Generates a hash code for the specified key in the current instance.
- key
- The Object whose hash code is to be generated.
A Int32 containing the hash code for key.
Exception Type Condition ArgumentNullException key is null
.
This method is accessible only through this class or a derived class.[Behaviors: As described above.]
[Default: If the current instance was instantiated with a specific IHashCodeProvider implementation, this method uses that hash code provider; otherwise, it uses the System.Object.GetHashCode implementation of key .]
System.Collections.Hashtable Class, System.Collections Namespace
protected virtual bool KeyEquals(object item, object key);
Determines whether the specified Object and the specified key in the current instance represent the same value.
- item
- The Object to compare with key.
- key
- The key in the current instance to compare with item.
true
if item and key represent the same value; otherwise,false
.
Exception Type Condition ArgumentNullException item is null
.-or-
key is
null
.
This method is accessible only through this class or a derived class.[Behaviors: As described above.]
[Default: If the current instance was initialized with a specified IComparer implementation, this method uses that implementation to perform the comparison; otherwise, the System.Object.Equals(System.Object) implementation of item is used.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual void Remove(object key);
Removes the entry with the specified key from the current instance.
- key
- The key of the entry to remove.
Exception Type Condition ArgumentNullException key is null
.NotSupportedException The current instance is read-only or has a fixed size.
[Note: This method is implemented to support the IDictionary interface.]
[Behaviors: As described above.]
[Default: This method uses the System.Object.Equals(System.Object) implementation of key to locate it in the current instance. If key is found in the current instance, the values of both key and its associated value are set to
null
. If key is not found in the current instance, no exception is thrown and the current instance remains unchanged.]
System.Collections.Hashtable Class, System.Collections Namespace
public static Hashtable Synchronized(Hashtable table);
Returns a synchronized (thread-safe) wrapper for the specified Hashtable.
- table
- The Hashtable to synchronize.
A synchronized (thread-safe) wrapper for table.
Exception Type Condition ArgumentNullException table is null
.
This method returns a new Hashtable instance that contains values equal to the values of table , and provides synchronized access to those values.If more than one thread is to write to a Hashtable concurrently, all write operations are required to be done through this wrapper.
[Note: A Hashtable can safely support one writer and multiple readers concurrently.]
System.Collections.Hashtable Class, System.Collections Namespace
IEnumerator IEnumerable.GetEnumerator();
Implemented to support the IEnumerable interface. [Note: For more information, see System.Collections.IEnumerable.GetEnumerator.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual int Count { get; }
Gets the number of key-and-value pairs contained in the current instance.
A Int32 that specifies the number of key-and-value pairs contained in the current instance.
This property is read-only.[Behaviors: As described above.]
System.Collections.Hashtable Class, System.Collections Namespace
int ICollection.Count { get; }
Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.Count.]
System.Collections.Hashtable Class, System.Collections Namespace
bool IDictionary.IsFixedSize { get; }
Implemented to support the IDictionary interface. [Note: For more information, see System.Collections.IDictionary.IsFixedSize.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual bool IsFixedSize { get; }
Gets a Boolean indicating whether the current instance has a fixed size.
true
if the current instance has a fixed size; otherwise,false
.
This property is read-only.[Note: Elements can be modified in, but not added to or removed from a Hashtable with a fixed size.]
[Behaviors: As described above.]
[Default: The default value of this property is
false
.]
[Overrides: Override this property, setting it to
true
, to prevent addition or removal of entries in the current instance.]
System.Collections.Hashtable Class, System.Collections Namespace
bool IDictionary.IsReadOnly { get; }
Implemented to support the IDictionary interface. [Note: For more information, see System.Collections.IDictionary.IsReadOnly.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual bool IsReadOnly { get; }
Gets a Boolean value indicating whether the current instance is read-only.
true
if the current instance is read-only; otherwise,false
.
This property is read-only.[Note: Elements cannot be modified in, added to, or removed from a Hashtable that is read-only.]
[Behaviors: As described above.]
[Default: The default value of this property is
false
.]
[Overrides: Override this property, setting it to
true
, in order to prevent the addition, removal, or modification of entries in the current instance.]
System.Collections.Hashtable Class, System.Collections Namespace
bool ICollection.IsSynchronized { get; }
Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.IsSynchronized.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual bool IsSynchronized { get; }
Gets a Boolean value indicating whether access to the current instance is synchronized (thread-safe).
true
if access to the current instance is synchronized (thread-safe); otherwise,false
.
This property is read-only.[Note: This property is implemented to support the ICollection interface.
For more information regarding synchronization of access to a Hashtable , see System.Collections.Hashtable.Synchronized(System.Collections.Hashtable) .
]
[Behaviors: As described above.]
[Default: The default value of this property is
false
.]
[Overrides: Override this property, setting it to
true
, if thread-safety can be guaranteed for the current instance. In order to obtain this safety, use System.Collections.Hashtable.SyncRoot or System.Collections.Hashtable.Synchronized(System.Collections.Hashtable) .]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual object this[object key] { get; set; }
Gets or sets the value in the current instance that is associated with the specified key.
- key
- The key whose value to get or set.
The value in the current instance that is associated with key. If key is not contained in the current instance, attempting to get it returnsnull
, and attempting to set it creates a new entry using key .
Exception Type Condition ArgumentNullException key is null
.NotSupportedException The property is being set and the current instance is read-only. The property is being set, key is not contained in the current instance, and the current instance has a fixed size.
[Note: This property provides the ability to access a specific element in the current instance using the following notation:myCollection[key]
.]
[Behaviors: As described above.]
[Default: If this property is being set and key is already contained in the current instance, the value associated with the old key is replaced.]
System.Collections.Hashtable Class, System.Collections Namespace
ICollection IDictionary.Keys { get; }
Implemented to support the IDictionary interface. [Note: For more information, see System.Collections.IDictionary.Keys.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual ICollection Keys { get; }
Gets a ICollection containing the keys of the current instance.
A ICollection containing the keys of the current instance.
This property is read-only.[Behaviors: As described above.]
[Default: The order of the keys in the ICollection is unspecified, but it is the same order as the associated values in the ICollection returned by the System.Collections.Hashtable.Values method.
The returned ICollection is a reference to the current instance, not a static copy. Therefore, changes to the current instance continue to be reflected in the ICollection.
]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual object SyncRoot { get; }
Gets a Object that can be used to synchronize access to the current instance.
A Object that can be used to synchronize access to the current instance.
This property is read-only.A thread is required to perform synchronized operations only on the System.Collections.Hashtable.SyncRoot of a Hashtable, not directly on the table itself. This maintains proper synchronization with any other threads concurrently modifying the table.
[Note: This property is implemented to support the ICollection interface.]
[Behaviors: As described above.]
[Default: This method returns a reference to the current instance.]
[Overrides: Override this property to return an object on which to lock when implementing a collection that wraps another collection (using a subset of it, for example). This is useful when providing synchronized access through two or more wrapper collections to the same underlying collection. Typically, this property returns a reference to the current instance.]
[Usage: Use this property to obtain a Object that can be used to synchronize access to the current instance.]
System.Collections.Hashtable Class, System.Collections Namespace
object ICollection.SyncRoot { get; }
Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.SyncRoot.]
System.Collections.Hashtable Class, System.Collections Namespace
public virtual ICollection Values { get; }
Gets a ICollection containing the values of the current instance.
A ICollection containing the values of the current instance.
This property is read-only.[Behaviors: As described above.]
[Default: The order of the values in the ICollection is unspecified, but it is the same order as the associated keys in the ICollection returned by the System.Collections.Hashtable.Keys method.
The returned ICollection is a reference to the current instance, not a static copy. Therefore, changes to the current instance continue to be reflected in the ICollection.
]
System.Collections.Hashtable Class, System.Collections Namespace
ICollection IDictionary.Values { get; }
Implemented to support the IDictionary interface. [Note: For more information, see System.Collections.IDictionary.Values.]
System.Collections.Hashtable Class, System.Collections Namespace