Standard Collection Interfaces
While IEnumerable
and IEnumerator
provide
standard ways to access the contents of a collection, they don’t provide
any way to modify it, nor any way to easily perform other common tasks, such
as determine the size, search the collection, etc. The .NET Framework also
defines a set of three standardized interfaces (ICollection
, IList
, and IDictionary
),
which collections should implement to provide support for these types of operations.
ICollection Interface
The ICollection
interface is the standard interface
for countable collections of objects. It provides the ability to determine
the size of a collection, to determine if it can be modified or synchronized,
and to copy the collection into an array for sequential processing. Since ICollection
extends IEnumerable
,
types that implement ICollection
can also be traversed
via IEnumerable
and IEnumerator
. The
interface looks like this:
public interface ICollection : IEnumerable { void CopyTo(Array array, int index); int Count {get;} bool IsReadOnly {get;} bool IsSynchronized {get;} object SyncRoot {get;} }
IList Interface
The IList
interface is the standard interface for
array-indexable collections. In addition to the functionality inherent in ICollection
and IEnumerable
, it also provides the ability to index directly into the collection by position (using the overloaded indexer), to add, remove and change elements in the collection by position, and to search for elements in the collection. The interface looks ...
Get C# in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.