Chapter 2. Collections, Enumerators, and Iterators

2.0 Introduction

Collections are groups of items; in .NET, collections contain objects, and each object contained in a collection is called an element. Some collections contain a straightforward list of elements, while others (dictionaries) contain a list of key/value pairs. The following collection types consist of a straightforward list of elements:

System.Collections.ArrayList
System.Collections.BitArray
System.Collections.Queue
System.Collections.Stack
System.Collections.Generic.LinkedList<T>
System.Collections.Generic.List<T>
System.Collections.Generic.Queue<T>
System.Collections.Generic.Stack<T>
System.Collections.Generic.HashSet<T>

The next set of collection types are all dictionaries:

System.Collections.Hashtable
System.Collections.SortedList
System.Collections.Generic.Dictionary<T,U>
System.Collections.Generic.SortedList<T,U>

This last collection type (HashSet<T>) can be thought of as a list of elements with no duplicates:

System.Collections.Generic.HashSet<T>

These collection classes are organized under the System.Collections and the System. Collections.Generic namespaces. In addition to these namespaces, there is a namespace called System.Collections.Specialized, which contains a few more useful collection classes. These classes might not be as well known as the previous classes, so here is a short explanation of them:

ListDictionary
This class operates similarly to the Hashtable. However, this class beats out ...

Get C# 6.0 Cookbook, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.