public interface ICollection : IEnumerable
|
IDictionary and IList are more specialized interfaces that are based on the ICollection interface. An IDictionary implementation is a collection of key-and-value pairs, like the Hashtable class. An IList implementation is a collection of values that can be sorted and whose members can be accessed by index, like the ArrayList class.
Some collections that limit access to their elements, like the Queue class and the Stack class, directly implement the ICollection interface.
If neither the IDictionary interface nor the IList interface meet the requirements of the required collection, derive the new collection class from the ICollection interface instead for more flexibility.
Count | Read-only When implemented by a class, gets the number of elements contained in the ICollection. |
IsSynchronized | Read-only When implemented by a class, gets a value indicating whether access to the ICollection is synchronized (thread-safe). |
SyncRoot | Read-only When implemented by a class, gets an object that can be used to synchronize access to the ICollection. |
CopyTo | When implemented by a class, copies the elements of the ICollection to an Array, starting at a particular Array index. |
Hierarchy:
int Count {get;}
|
bool IsSynchronized {get;}
|
Most collection classes in the System.Collections namespace also implement a Synchronized method, which provides a synchronized wrapper around the underlying collection.
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 ICollection.SyncRoot during the entire enumeration:
ICollection myCollection = new ICollection(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
object SyncRoot {get;}
|
Most collection classes in the System.Collections namespace also implement a Synchronized method, which provides a synchronized wrapper around the underlying collection. However, derived classes can provide their own synchronized version of the collection using the ICollection.SyncRoot property. The synchronizing code must perform operations on the ICollection.SyncRoot of the collection, not directly on the collection. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the collection instance.
In the absence of a Synchronized method on a collection, the expected usage for ICollection.SyncRoot looks like this:
ICollection MyCollection = ... lock( MyCollection.SyncRoot ) { // Some operation on the collection, which is now thread-safe. }
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 ICollection.SyncRoot during the entire enumeration:
ICollection myCollection = new ICollection(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
array
index
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | index is less than zero. |
ArgumentException | array is multidimensional. -or- index is equal to or greater than the length of array. -or- The number of elements in the source ICollection is greater than the available space from index to the end of the destination array. |
InvalidCastException | The type of the source ICollection cannot be cast automatically to the type of the destination array. |