[Serializable] |
This implementation does not provide a synchronized (thread-safe) wrapper for a HybridDictionary, but derived classes can create their own synchronized versions of the HybridDictionary using the HybridDictionary.SyncRoot property.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
If the initial size of the collection is greater than the optimal size for a ListDictionary, the collection is stored in a Hashtable right away to avoid the overhead of copying elements from the ListDictionary to a Hashtable.
The constructor accepts a boolean parameter that allows the user to specify whether the collection ignores the case when comparing strings. If the collection is case-sensitive, it uses the key's implementations of Object.GetHashCode and Object.Equals. If the collection is case-insensitive, it performs a simple ordinal case-insensitive comparison, which obeys the casing rules of the invariant culture only. By default, the collection is case-sensitive. For more information on the invariant culture, see CultureInfo.
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Creates an empty case-sensitive HybridDictionary. |
ctor #2 | Overloaded:.ctor(bool caseInsensitive) Creates an empty HybridDictionary with the specified case-sensitivity. |
ctor #3 | Overloaded:.ctor(int initialSize) Creates a case-sensitive HybridDictionary with the specified initial size. |
ctor #4 | Overloaded:.ctor(int initialSize, bool caseInsensitive) Creates a HybridDictionary with the specified initial size and case-sensitivity. |
Count | Read-only Gets the number of key-and-value pairs contained in the HybridDictionary. |
IsFixedSize | Read-only Gets a value indicating whether the HybridDictionary has a fixed size. |
IsReadOnly | Read-only Gets a value indicating whether the HybridDictionary is read-only. |
IsSynchronized | Read-only Gets a value indicating whether the HybridDictionary is synchronized (thread-safe). |
Item | Read-write Gets or sets the value associated with the specified key. |
Keys | Read-only Gets an ICollection containing the keys in the HybridDictionary. |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the HybridDictionary. |
Values | Read-only Gets an ICollection containing the values in the HybridDictionary. |
Add | Adds an entry with the specified key and value into the HybridDictionary. |
Clear | Removes all entries from the HybridDictionary. |
Contains | Determines whether the HybridDictionary contains a specific key. |
CopyTo | Copies the HybridDictionary entries to a one-dimensional Array instance at the specified index. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetEnumerator | Returns an IDictionaryEnumerator that can iterate through the HybridDictionary. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Remove | Removes the entry with the specified key from the HybridDictionary. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
IEnumerable.GetEnumerator | Returns an IEnumerator that can iterate through the HybridDictionary. |
Hierarchy:
public HybridDictionary(); |
The comparer determines whether two keys are equal. Every key in a HybridDictionary must be unique.
public HybridDictionary( |
caseInsensitive
public HybridDictionary( |
initialSize
By default, the collection is case-sensitive and uses the key's implementation of Object.GetHashCode as the hash code provider and the key's implementation of Object.Equals as the comparer.
The comparer determines whether two keys are equal. Every key in a HybridDictionary must be unique.
initialSize
caseInsensitive
If caseInsensitive is false, the collection uses the key's implementations of Object.GetHashCode and Object.Equals. If caseInsensitive is true, the collection performs a simple ordinal case-insensitive comparison, which obeys the casing rules of the invariant culture only. For more information on the invariant culture, see CultureInfo.
public int Count {get;}
|
public bool IsFixedSize {get;}
|
A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but it allows the modification of existing elements.
public bool IsReadOnly {get;}
|
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.
public bool IsSynchronized {get;}
|
Derived classes can provide a synchronized version of the HybridDictionary using the HybridDictionary.SyncRoot property.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the HybridDictionary.SyncRoot during the entire enumeration:
HybridDictionary myCollection = new HybridDictionary(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public object this[object key] {get; set;}
|
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
myCollection[key]
.When setting this property, if the specified key already exists in the HybridDictionary, the value is replaced; otherwise, a new element is created. In contrast, the HybridDictionary.Add method does not modify existing elements.
public ICollection Keys {get;}
|
The returned ICollection is a reference to the original HybridDictionary, not a static copy. Therefore, changes to the HybridDictionary continue to be reflected in the ICollection.
public object SyncRoot {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the HybridDictionary.SyncRoot during the entire enumeration:
HybridDictionary myCollection = new HybridDictionary(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public ICollection Values {get;}
|
The returned ICollection is a reference to the original HybridDictionary, not a static copy. Therefore, changes to the HybridDictionary continue to be reflected in the ICollection.
key
value
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
ArgumentException | An entry with the same key already exists in the HybridDictionary. |
The HybridDictionary.Item property can also be used to add new elements by setting the value of a key that does not exist in the HybridDictionary. For example:
myCollection["myNonexistentKey"] = myValue
. However, if the specified key already exists in the HybridDictionary, setting the HybridDictionary.Item property overwrites the old value. In contrast, the HybridDictionary.Add method does not modify existing elements.
When the number of elements becomes greater than the optimal size for a ListDictionary, the elements are copied from the ListDictionary to a Hashtable. However, this only happens once. If the collection is already stored in a Hashtable and the number of elements falls below the optimal size for a ListDictionary, the collection remains in the Hashtable.
public void Clear(); |
If the collection is already stored in a Hashtable, the collection remains in the Hashtable.
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
array
index
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | index is less than zero. |
ArgumentException | array is multidimensional. -or- arrayIndex is equal to or greater than the length of array. -or- The number of elements in the source HybridDictionary is greater than the available space from arrayIndex to the end of the destination array. |
InvalidCastException | The type of the source HybridDictionary cannot be cast automatically to the type of the destination array. |
To copy only the keys in the HybridDictionary, use
HybridDictionary.Keys.CopyTo
.
To copy only the values in the HybridDictionary, use
HybridDictionary.Values.CopyTo
.
~HybridDictionary(); |
public IDictionaryEnumerator GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.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 the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public void Remove( |
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
If the collection is already stored in a Hashtable and the number of elements falls below the optimal size for a ListDictionary, the collection remains in the Hashtable to avoid the overhead of copying elements from the Hashtable back to a ListDictionary.
IEnumerator IEnumerable.GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.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 the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
public virtual string ToString(); |