[Serializable] |
This implementation does not provide a synchronized (thread-safe) wrapper for a ListDictionary, but derived classes can create their own synchronized versions of the ListDictionary using the ListDictionary.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.
Members, such as ListDictionary.Item, ListDictionary.Add, ListDictionary.Remove, and ListDictionary.Contains are O(n) operations, where n is ListDictionary.Count.
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Creates an empty ListDictionary using the default comparer. |
ctor #2 | Overloaded:.ctor(IComparer comparer) Creates an empty ListDictionary using the specified comparer. |
Count | Read-only Gets the number of key-and-value pairs contained in the ListDictionary. |
IsFixedSize | Read-only Gets a value indicating whether the ListDictionary has a fixed size. |
IsReadOnly | Read-only Gets a value indicating whether the ListDictionary is read-only. |
IsSynchronized | Read-only Gets a value indicating whether the ListDictionary 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 ListDictionary. |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the ListDictionary. |
Values | Read-only Gets an ICollection containing the values in the ListDictionary. |
Add | Adds an entry with the specified key and value into the ListDictionary. |
Clear | Removes all entries from the ListDictionary. |
Contains | Determines whether the ListDictionary contains a specific key. |
CopyTo | Copies the ListDictionary 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 ListDictionary. |
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 ListDictionary. |
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 ListDictionary. |
Hierarchy:
public ListDictionary(); |
public ListDictionary( |
comparer
-or-
null to use the default comparer, which is each key's implementation of Object.Equals.
The IComparer to use to determine whether two keys are equal.-or-
null to use the default comparer, which is each key's implementation of Object.Equals.
The custom comparer enables such scenarios as doing lookups with case-insensitive strings.
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 ListDictionary using the ListDictionary.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 ListDictionary.SyncRoot during the entire enumeration:
ListDictionary myCollection = new ListDictionary(); 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 ListDictionary, the value is replaced; otherwise, a new element is created. In contrast, the ListDictionary.Add method does not modify existing elements.
This is an O(n) operation, where n is ListDictionary.Count.
public ICollection Keys {get;}
|
The returned ICollection is a reference to the original ListDictionary, not a static copy. Therefore, changes to the ListDictionary 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 ListDictionary.SyncRoot during the entire enumeration:
ListDictionary myCollection = new ListDictionary(); 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 ListDictionary, not a static copy. Therefore, changes to the ListDictionary 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 ListDictionary. |
The ListDictionary.Item property can also be used to add new elements by setting the value of a key that does not exist in the ListDictionary. For example:
myCollection["myNonexistentKey"] = myValue
. However, if the specified key already exists in the ListDictionary, setting the ListDictionary.Item property overwrites the old value. In contrast, the ListDictionary.Add method does not modify existing elements.
This is an O(n) operation, where n is ListDictionary.Count.
public void Clear(); |
This is an O(1) operation.
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 ListDictionary is greater than the available space from arrayIndex to the end of the destination array. |
InvalidCastException | The type of the source ListDictionary cannot be cast automatically to the type of the destination array. |
To copy only the keys in the ListDictionary, use
ListDictionary.Keys.CopyTo
.
To copy only the values in the ListDictionary, use
ListDictionary.Values.CopyTo
.
~ListDictionary(); |
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. |
This is an O(n) operation, where n is ListDictionary.Count.
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(); |