Collection Basics

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
MemberDescription
GetEnumeratorThis method returns an Enumerator that can be used to iterate over a collection. This method is inherited from the IEnumerable interface.
CountThis property gets the number of elements contained in the collection.
IsSynchronizedThis property indicates whether the class is thread safe.
SyncRootThis property can be used to get an object to synchronize the collection.
CopyToThis method copies the elements of the collection to an array.

Table 5.3 lists the collection interfaces.

Table 5.3. Collection Interfaces
InterfaceDescription
ICollectionProvides a standard interface for all collections implemented in C#
IComparerProvides the ability for a collection to sort the items contained within its collection
IDictionaryRepresents a collection that contains key/value pairs
IDictionaryEnumeratorProvides the ability for a key/value collection to sort the items contained within its collection
IEnumerableProvides the ability for a collection to iterate over the items contained within its collection
IListProvides 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.

ENUMERATORS

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
NameDescription
CurrentThis property returns the current object in the collection.
MoveNextThis method moves the enumerator to the next item in the collection.
ResetThis 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();
  while(enumerator.MoveNext())
  {
    System.Console.WriteLine((string) enumerator.Current);
  }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.114.29