Collection classes can be grouped into collections that store elements of type Object and generic collection classes. Previous to .NET 2.0, generics didn’t exist. Now the generic collection classes usually are the preferred type of collection. Generic collection classes are type-safe, and there is no boxing if value types are used. You only need object-based collection classes if you want to add objects of different types where the types are not based on each other, for example, adding int and string objects to a collection. Another group of collection classes is collections specialized for a specific type; for example, the StringCollection class is specialized for the string type.
Tip | You can read all the information about generics in Chapter 9, “Generics.” |
Object-type collections are located in the namespace System.Collections; generic collection classes can be found in the namespace System.Collections.Generic. Collection classes that are specialized for a specific type are located in the namespace System.Collections.Specialized.
Of course, there are also other ways to group collection classes. Collections can be grouped into lists, collections, and dictionaries based on the interfaces that are implemented by the collection class. Interfaces and their functionality are described in the following table. .NET 2.0 adds new generic interfaces for collection classes, for example, IEnumerable<T> and IList<T>. While the nongeneric versions of these interfaces define an Object as parameter of the methods, the generic version of these interfaces use the generic type T.
Tip | You can read detailed information about the interfaces IEnumerable, ICollection, and IList in Chapter 5, “Arrays.” |
Interfaces and their methods and properties that are important for collections are described in the following table.
Interface | Methods and Properties | Description |
---|---|---|
IEnumerable, IEnumerable<T> | GetEnumerator() | The interface IEnumerable is required if a foreach statement is used with the collection. This interface defines the method GetEnumerator(), which returns an enumerator that implements IEnumerator. The generic interface IEnumerable<T> inherits from the nongeneric interface IEnumerable, and defines a GetEnumerator method to return Enumerator<T>. Because of the inheritance with these two interfaces, with every method that requires a parameter of type IEnumerable you can also pass IEnumerable<T> objects. |
ICollection | Count, IsSynchronized, SyncRoot, CopyTo() | The interface ICollection is implemented by collection classes. Methods of this interface can be used to add and remove elements to and from the collection. |
ICollection<T> | Count IsReadOnly, Add(), Clear(), Contains(), CopyTo(), Remove() | The interface ICollection extends the functionality from the interface IEnumerable. |
IList | IsFixedSize, IsReadOnly, Item, Add, Clear, Contains, IndexOf, Insert, Remove, RemoveAt | The interface IList derives from the interface ICollection. IList allows you to access a collection using an indexer. It is also possible to insert or remove elements at any position of the collection. Similar to ICollection<T>, the interface IList<T> inherits from ICollection. |
IList<T> | Item, IndexOf, Insert, RemoveAt | In Chapter 5, “Arrays,” you saw that the Array class implements this interface, but methods to add or remove elements throw a NotSupportedException. Collections that have a fixed size (e.g., the Array class) and are readonly can throw a NotSupportedException with some of the methods defined in this interface. |
IDictionary | IsFixedSize, IsReadOnly, Item, Keys, Values, Add(), Clear(), Contains(), GetEnumerator(), Remove() | The interface IDictionary or IDictionary<TKey, TValue> is implemented by collections whose elements have a key and a value. |
IDictionary<TKey, TValue> | Item, Keys, Values, Add(), ContainsKey(), Remove(), TryGetValue() | |
IComparer<T> | Compare() | The interface IComparer<T> is implemented by a comparer and used to sort elements inside a collection with the Compare() method. |
IEqualityComparer<T> | Equals(), GetHashCode() | IEqualityComparer<T> is implemented by a comparer that can be used for keys in a dictionary. With this interface the objects can be compared for equality. The method GetHashCode() should return a unique value for every object. The method Equals() returns true if the objects are equal, and false otherwise. |