Collection Interfaces
The .NET Framework provides two sets of standard interfaces for enumerating and comparing collections: the traditional (nontype-safe) and the new generic type-safe collections. This book focuses only on the new type-safe collection interfaces, as these are far preferable.
You can declare an ICollection
of any specific type by substituting the actual type (e.g., int
or string
) for the generic type in the interface declaration (<T>
).
Tip
C++ programmers take note: C# Generics are similar in syntax and usage to C++ templates. However, because the generic types are expanded to their specific type at runtime, the JIT compiler is able to share code among different instances, dramatically reducing the code bloat that you may see when using templates in C++.
Table 9-2 lists the key generic collection interfaces.[13]
Table 9-2. Collection interfaces
Interface | Purpose |
---|---|
| Base interface for generic collections |
| Enumerate through a collection using a |
| Implemented by all collections to provide the |
| Compare two objects held in a collection so that the collection can be sorted |
| Used by array-indexable collections |
| Used for key-/value-based collections such as |
The IEnumerable<T> Interface
You can support the foreach
statement in ListBoxTest
by implementing the IEnumerable<T>
interface ...
Get Programming C# 3.0, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.