System.Collections.ICollection Interface

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Defines size, enumerators and synchronization methods for all collections.
C# Syntax:
public interface ICollection : IEnumerable
Remarks
The ICollection interface is the base interface for classes in the System.Collections namespace.

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.

See also:
System.Collections Namespace | IDictionary | IList

System.Collections.ICollection Member List:

Public Properties
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.
Public Methods
CopyTo When implemented by a class, copies the elements of the ICollection to an Array, starting at a particular Array index.

Hierarchy:


System.Collections.ICollection Member Details

Property: Count (read-only)
Summary
When implemented by a class, gets the number of elements contained in the ICollection.
C# Syntax:
int Count {get;}

Return to top


Property: IsSynchronized (read-only)
Summary
When implemented by a class, gets a value indicating whether access to the ICollection is synchronized (thread-safe).
C# Syntax:
bool IsSynchronized {get;}
Remarks
ICollection.SyncRoot returns an object, which can be used to synchronize access to the ICollection.

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.
               }
              }
            
See also:
ICollection.SyncRoot

Return to top


Property: SyncRoot (read-only)
Summary
When implemented by a class, gets an object that can be used to synchronize access to the ICollection.
C# Syntax:
object SyncRoot {get;}
Remarks
For collections whose underlying store is not publicly available, the expected implementation is to return the current instance. Note that the pointer to the current instance might not be sufficient for collections that wrap other collections; those should return the underlying collection's SyncRoot property.

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.
               }
              }
            
See also:
ICollection.IsSynchronized

Return to top


Method: CopyTo(
   Array array,
   int index
)
Summary
When implemented by a class, copies the elements of the ICollection to an Array, starting at a particular Array index.
C# Syntax:
void CopyTo(
   Array array,
   int index
);
Parameters:

array

The one-dimensional Array that is the destination of the elements copied from ICollection. The Array must have zero-based indexing.

index

The zero-based index in array at which copying begins.

Exceptions
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.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.