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
|
Member
|
Description
|
|
GetEnumerator
|
This method returns an
Enumerator
that
can be used to iterate over a collection. This method is inherited
from the
IEnumerable
interface.
|
|
Count
|
This property gets the number of elements
contained in the collection.
|
|
IsSynchronized
|
This property indicates whether the class is
thread safe.
|
|
SyncRoot
|
This property can be used to get an object to
synchronize the collection.
|
|
CopyTo
|
This method copies the elements of the
collection to an array.
|
Table 5.3 lists the collection interfaces.
Table 5.3. Collection Interfaces
|
Interface
|
Description
|
|
ICollection
|
Provides a standard interface for all
collections implemented in C#
|
|
IComparer
|
Provides the ability for a collection to
sort
the items contained within its collection
|
|
IDictionary
|
Represents a collection that contains key/value
pairs
|
|
IDictionaryEnumerator
|
Provides the ability for a key/value collection
to sort the items contained within its collection
|
|
IEnumerable
|
Provides the ability for a collection to iterate
over the items contained within its collection
|
|
IList
|
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
|
Name
|
Description
|
|
Current
|
This property returns the current object in the
collection.
|
|
MoveNext
|
This method moves the enumerator to the next
item in the collection.
|
|
Reset
|
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();
while(enumerator.MoveNext())
{
System.Console.WriteLine((string) enumerator.Current);
}
}
|