Collections are groups of items or as in C#, objects. In C# .NET, each built-in collection implements the ICollection interface. Because the ICollection interface inherits from the IEnumerable interface, each built-in collection also implements the IEnumerable interface. In the following sections, you will gain a basic knowledge of what collections are and how they operate.
Understanding the Basic Collection Interfaces
These are collections that are provided with C#, out of the box; they all reside in the System.Collections namespace. Each implements the ICollection interface. Table 5.2 shows the members of the ICollection interface.
Table 5.2. ICollection Members
This method returns an Enumerator that can be used to iterate over a collection. This method is inherited from the IEnumerable interface.
This property gets the number of elements contained in the collection.
This property indicates whether the class is thread safe.
This property can be used to get an object to synchronize the collection.
This method copies the elements of the collection to an array.
Table 5.3 lists the collection interfaces.
Table 5.3. Collection Interfaces
Provides a standard interface for all collections implemented in C#
Provides the ability for a collection to sort the items contained within its collection
Represents a collection that contains key/value pairs
Provides the ability for a key/value collection to sort the items contained within its collection
Provides the ability for a collection to iterate over the items contained within its collection
Provides the ability for a collection to be accessed by an index
Iterating Through Collections
Because the ICollection interface inherits from the IEnumerable interface, collections provide the ability to iterate over the items that the collection contains.
Iterating through a collection can be done in a few different ways. Because all collections implement the IEnumerable interface, you can use the IEnumberable.GetEnumerator to return an enumerator that is capable of iterating through the collection.
An enumerator is an object that enables you to iterate through the items contained within a collection. An enumerator can be used only to read the values of a collection; it cannot be used to change the contents of the collection. When an enumerator is first instantiated, it is positioned before the first element of the collection. If you try to access the first element, using Current, before calling the MoveNext method, an exception will be thrown. Call MoveNext to move the enumerator to the next element of the collection. When the enumerator reaches the end of the collection, it will be positioned after the last element of the collection and will return false. Again, if you call Current when the enumerator is positioned after the last element in the collection, an exception will be thrown.
Table 5.4. Methods and Properties of an Enumerator
This property returns the current object in the collection.
This method moves the enumerator to the next item in the collection.
This method moves the enumerator to its initial position.
The following code fragment demonstrates how to iterate through a collection:
protected void PrintList(System.Collections.ArrayList list)
IEnumerator enumerator = list.GetEnumerator();