Collections are data structures that holds data in different ways for flexible operations. Non-generic collections store items as Object. The runtime library Collection interfaces are defined as part of the CNClrLib.Collections namespace. The following are list of some of the non-generic collection types. with code examples.
ArrayList Collection
An ArrayList is a dynamic array. It provides random access to its elements. An ArrayList automatically expands as data is added. Unlike arrays, an ArrayList can hold data of multiple data types. Elements in the ArrayList are accessed via an integer index. Indexes are zero based. Indexing of elements and insertion and deletion at the end of the ArrayList takes constant time. Inserting or deleting an element in the middle of the dynamic array is more costly. It takes linear time. The example below demonstrates how to create, add, remove and access elements of an arraylist:
Hashtable Collection
The Hashtable represents a collection of key/value pairs that are organized based on the hash code of the key. The following example shows how to create, initialize and perform various functions to a Hashtable and how to print out its keys and values.
Queue Collection
The Queue represents a first-in, first-out collection of objects. The queue is implemented as a circular array. Objects stored in a Queue are inserted at one end and removed from the other. Queues are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Queue if you need to access the information in the same order that it is stored in the collection. Queue accepts null as a valid value and allows duplicate elements. The following example shows how to create and add values to a Queue and how to print out its values.
Stack Collection
The stack represents a simple last-in-first-out (LIFO) non-generic collection of objects. Stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Stack if you need to access the information in reverse order. Stack accepts null as a valid value and allows duplicate elements. The following example shows how to create and add values to a Stack and how to display its values.
SortedList Collection
The SortedList represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. A SortedList element can be accessed by its key, like an element in any IDictionary implementation, or by its index, like an element in any IList implementation.. The following code example shows how to create and initialize a SortedList object and how to print out its keys and values.
BitArray Collections
The BitArray manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). The BitArray is a collection in which the capacity is always the same as the count. Elements are added to a BitArray by increasing the Length property; elements are deleted by decreasing the Length property. The following code example shows how to create and initialize a BitArray and how to print out its values.
Comparer Interface
Compares two objects for equivalence, where string comparisons are case-sensitive. The following code example shows how Compare returns different values depending on the culture associated with the Comparer.