.Net Runtime Library for Delphi
Close
Collections and Data Structures

Similar data can often be handled more efficiently when stored and manipulated as a collection. You can use the Array class or the interfaces in CNClrLib.Collections namespace to add, remove, and modify either individual elements or a range of elements in a collection.

There are two main types of collections; generic collections and non-generic collections. Generic collections were added in the .NET Framework 2.0 and provide collections that are type-safe at compile time. Because of this, generic collections typically offer better performance. Generic collections accept a type parameter when they are constructed and do not require that you cast to and from the Object type when you add or remove items from the collection. Non-generic collections store items as Object.

All collections provide methods for adding, removing or finding items in the collection. In addition, all collections that directly or indirectly implement the ICollection interface share these features:

  • The ability to enumerate the collection

    Each Collection interface that implements System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> contains AsIEnumerable method which allows the collection to be iterated through. An enumerator can be thought of as a movable pointer to any element in the collection.

  • The ability to copy the collection contents to an array

    All collections can be copied to an array using the CopyTo method; however, the order of the elements in the new array is based on the sequence in which the enumerator returns them. The resulting array is always one-dimensional with a lower bound of zero.

In addition, many collection classes contain the following features:

  • Capacity and Count properties

    The capacity of a collection is the number of elements it can contain. The count of a collection is the number of elements it actually contains. Some collections hide the capacity or the count or both.

    A BitArray interface is a special case; its capacity is the same as its length, which is the same as its count.

  • A consistent lower bound

    The lower bound of a collection is the index of its first element. All indexed collections in the CNClrLib.Collections namespace have a lower bound of zero, meaning they are 0-indexed. Array has a lower bound of zero by default, but a different lower bound can be defined when creating an instance of the Array class or interface.

  • Synchronization for access from multiple threads

    Non-generic collection types in the CNClrLib.Collections namespace provide some thread safety with synchronization; typically exposed through the SyncRoot and IsSynchronized members.

In general, you should use generic collections. The following table describes some common collection scenarios and the collection classes you can use for those scenarios. If you are new to generic collections, this table will help you choose the generic collection that works the best for your task.

I want to.

Generic collection option(s)

Non-generic collection option(s)

Thread-safe or immutable collection option(s)

Store items as key/value pairs for quick look-up by key

_GenericDictionary

_Hashtable

(A collection of key/value pairs that are organize based on the hash code of the key.)

_GenericConcurrentDictionary

_GenericReadOnlyDictionary

Access items by index

_GenericList

_Array Interface/TClrArray Class

_ArrayList

No recommendation

Use items first-in-first-out (FIFO)

_GenericQueue

_Queue

_GenericConcurrentQueue

Use data Last-In-First-Out (LIFO)

_GenericStack

_Stack

_GenericConcurrentStack

Access items sequentially

_GenericLinkedList

No recommendation

No recommendation

Receive notifications when items are removed or added to the collection.

_GenericObservableCollection

No recommendation

No recommendation

A sorted collection

_GenericSortedList

_SortedList

No recommendation

A set for mathematical functions

_GenericHashSet

_GenericSortedSet

No recommendation

No recommendation