8.3. Generic Collection Classes

Now that you have a high-level view of the generics namespaces, it's time to look at the nuts-and-bolts details of each individual class. The idea here is to go through the members of every class and provide an overview of the purpose and usage of each member. Each class in this section is accompanied by samples that illustrate some of the key concepts associated with that specific class.

8.3.1. Collection<T>

The Collection<T> class is an index-based, dynamically sized collection that provides you with all the fundamental interfaces you need to add, remove, and randomly access the elements of a collection. Its purpose is to provide a lightweight interface for constructing and maintaining a collection that can also serve as the base class for any user-defined, custom collections. The following table shows a complete list of methods and properties for Collection<T>.

Method or Property NameDescription
Add()Adds the provided object to the end of the collection.
Clear()Removes all the items from the collection.
Contains()Attempts to find an object in the collection equal to the passed in object. If a matching object is found, the method returns true.
CopyTo()Copies the contents of the collection to an Array.
CountReturns the number of items currently stored in the collection.
GetEnumerator()Gets the IEnumerator<T> enumerator for your collection.
IndexOf()Finds the index of the object equal to the object supplied as a parameter. If no matching object is found, ...

Get Professional .NET 2.0 Generics 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.