O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required