The Generic Interfaces
System.Collections.Generic
defines a number of generic interfaces, all of which parallel
their corresponding non-generic counterparts. The generic interfaces are summarized in
Table 24-10.
The ICollection Interface
The
ICollection
interface defines those features that all generic collections have in
common. It inherits the
IEnumerable
and
IEnumerable
interfaces.
ICollection
is
the generic version of the non-generic
ICollection
interface. However, there are some
differences between the two.
ICollection
defines the following properties:
int Count { get; }
bool IsReadOnly { get; }
Count
contains the number of items currently held in the collection.
IsReadOnly
is true if
the collection is read-only. It is false if the collection is read/write.
Interface
Description
ICollection
Defines the foundational features for the generic collections.
IComparer
Defines the generic
Compare( )
method that per forms a
comparison on objects stored in a collection.
IDictionar y
Defines a generic collection that consists of key/value pairs.
IEnumerable
Defines the generic
GetEnumerator( )
method, which supplies the
enumerator for a collection class.
IEnumerator
Provides members that enable the contents of a collection to be
obtained one at a time.
IEqualityComparer
Compares two objects for equality.
IList
Defines a generic collection that can be accessed via an indexer.
T
ABLE
24-10
The Generic Collection Inter faces
www.freepdf-books.com
776
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
ICollection
defines the following methods. Notice it defines a few more methods
than does its non-generic counterpart.
Method
Description
void Add(T
obj
)
Adds
obj
to the invoking collection. Throws a
NotSupportedException
if the collection is read-only.
void Clear( )
Deletes all elements from the invoking collection and
sets
Count
to zero.
bool Contains(T
obj
)
Returns true if the invoking collection contains the
object passed in
obj
and false other wise.
void CopyTo(T[ ]
target
, int
star tIdx
)
Copies the contents of the invoking collection to
the array specified by
target,
beginning at the index
specified by
star tIdx.
bool Remove(T
obj
)
Removes the first occurrence of
obj
from the invoking
collection. Returns true if
obj
was removed and false if
it was not found in the invoking collection.
Several of these methods will throw
Do'stlaringiz bilan baham: |