Predefined Collection Classes

The FCL provides a reasonably comprehensive set of prebuilt data structures providing concrete implementations of all the interfaces described in this chapter. However, since C# does not yet support generics, the implementations work in terms of the generic object type, which has the same disadvantages (excessive casting, boxing) as the generic IEnumerator interface described earlier. If you prefer more type-safe collection classes, you may use one of these predefined types as a starting point for your own type-safe variant.

The Array Class

The Array class is the canonical aggregate data structure in the FCL, representing a fixed-size array of object references of uniform type. Since the Array data structure is fundamental, the C# language provides explicit array declaration and initialization syntax (for more details, see Chapter 2 and Chapter 3). The storage for the array is allocated on the GC heap at the time a class is instantiated, and cannot change (see the ArrayList class for a growable array-like data structure). The Array class implements ICollection, IList, and IEnumerable, so arrays can be treated as lists, generic cloneable collections, or sets of elements that can be enumerated. In addition, the Array class supports sorting and searching of the array. Sorting of the array is often accomplished by delegating to IComparable implementations on the contained elements, which requires that you implement IComparable on types that are intended for ...

Get C# in a Nutshell 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.