ICollection Interface
ICollection
Represents the base interface for all collection types in the .NET Framework. This interface provides standard methods and properties that allow access to the elements in a collection, as well as information about the collection itself.
Properties
Count
Gets the number of elements contained in the ICollection.
IsSynchronized
Gets a value indicating whether access to the ICollection is synchronized (thread-safe).
SyncRoot
Gets an object that can be used to synchronize access to the ICollection.
Methods
CopyTo
Copies the entire ICollection to a compatible one-dimensional Array, starting at the specified index of the target Array.
- array
- The one-dimensional Array that is the destination of the elements copied from the ICollection. The Array must have zero-based indexing.
- index
- The zero-based index in array at which the elements will be copied.
Properties
Count
Gets the number of elements contained in the ICollection.
IsSynchronized
Gets a value indicating whether access to the ICollection is synchronized (thread-safe).
SyncRoot
Gets an object that can be used to synchronize access to the ICollection.
Methods
CopyTo
Copies the entire ICollection to a compatible one-dimensional Array, starting at the specified index of the target Array.
- array
- The one-dimensional Array that is the destination of the elements copied from the ICollection. The Array must have zero-based indexing.
- index
- The zero-based index in array at which the elements will be copied.
Implements
This interface is implemented by the following types:
Remarks
The ICollection interface is the base for collections that can be accessed by index. It provides basic properties for managing the size and synchronization of a collection.
Implementers of ICollection must also implement IEnumerable to provide an enumerator for the collection.
The ICollection interface is used to group common operations on collections, such as retrieving the number of items, checking for thread safety, and copying elements to an array.
Thread Safety
For collections that are not thread-safe, implementers should ensure that the SyncRoot property returns a reference to the object that is used to synchronize access to the collection, and that synchronization is handled correctly in methods that modify the collection.
Examples
Using ICollection to copy elements to an array
using System;
using System.Collections;
public class Example
{
public static void Main(string[] args)
{
ArrayList myList = new ArrayList() { 1, 2, 3, 4, 5 };
// Create an array to hold the elements.
// The array size must be at least the Count of the ICollection.
int[] myArray = new int[myList.Count];
// Copy elements from the ArrayList (which implements ICollection) to the array.
myList.CopyTo(myArray, 0);
Console.WriteLine("Elements copied to array:");
foreach (int item in myArray)
{
Console.Write(item + " ");
}
// Output: Elements copied to array: 1 2 3 4 5
}
}
XML Documentation
This is a representation of the XML documentation for the ICollection interface.
<doc>
<members>
<member name="T:System.Collections.ICollection">
<summary>Represents the base interface for all collection types in the .NET Framework.</summary>
<remarks>
The <see langword="ICollection"> interface is the base for collections that can be accessed by index. It provides basic properties for managing the size and synchronization of a collection.
Implementers of <see langword="ICollection"> must also implement <see langword="IEnumerable"> to provide an enumerator for the collection.
The <see langword="ICollection"> interface is used to group common operations on collections, such as retrieving the number of items, checking for thread safety, and copying elements to an array.
For collections that are not thread-safe, implementers should ensure that the <see langword="SyncRoot"> property returns a reference to the object that is used to synchronize access to the collection, and that synchronization is handled correctly in methods that modify the collection.
</remarks>
</member>
<member name="P:System.Collections.ICollection.Count">
<summary>Gets the number of elements contained in the <see langword="ICollection">.</summary>
</member>
<member name="P:System.Collections.ICollection.IsSynchronized">
<summary>Gets a value indicating whether access to the <see langword="ICollection"> is synchronized (thread-safe).</summary>
</member>
<member name="P:System.Collections.ICollection.SyncRoot">
<summary>Gets an object that can be used to synchronize access to the <see langword="ICollection">.</summary>
</member>
<member name="M:System.Collections.ICollection.CopyTo(System.Array,System.Int32)">
<summary>Copies the entire <see langword="ICollection"> to a compatible one-dimensional <see langword="Array">, starting at the specified index of the target <see langword="Array">.</summary>
<param name="array">The one-dimensional <see langword="Array"> that is the destination of the elements copied from the <see langword="ICollection">. The <see langword="Array"> must have zero-based indexing.</param>
<param name="index">The zero-based index in array at which the elements will be copied.</param>
</member>
</members>
</doc>