.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple

Collections. At its simplest, an object holds a single value. At its most complex, it holds references, often to many other objects. The .NET Framework provides many collections.

List. This is an efficient, dynamically-allocated array. It does not provide fast lookup in the general case (the Dictionary is better for lookups). List is often used in loops.


Dictionary. This is an implementation of a hash table: an extremely efficient way to store keys for lookup. Dictionary is fast, well-designed and reliable.


ArrayList. This is a collection found in System.Collections. It stores objects of any type. There is no need to worry about the types of elements. But we must cast them.


Hashtable. This is a lookup data structure. It uses a hash code to quickly find elements. Dictionary is usually more appropriate for programs.


Concurrent. Does a program use threads? If so, consider the ConcurrentDictionary. It improves thread-safety. ConcurrentBag offers a simple way to share data between threads.


BitArray is an abstract data type for representing bit data. It uses an efficient representation of the underlying bits. It is a good choice when using many boolean values.


Tuple. We use this to store data. It has Items. These are strongly-typed fields. We can use a Tuple instead of a custom class, but it cannot be changed after creation.


KeyValuePair is a useful data type. With it, we store a key and a value together in a struct. No custom struct is needed. It is a simple form of a Tuple. The Dictionary collection uses it.

KeyValuePairTuple vs. KeyValuePair

Stack. We push and pop elements onto the top of a Stack. With this functionality, we implement certain kinds of parsers. An array would work, but the Stack has a clearer interface.


Queue. This removes elements that were added first (FIFO, first-in-first-out). So we keep items in order and service the ones that were added first, like a line at a government agency.


Sets. The HashSet implements set logic in its many instance methods. We use methods such as Union on different HashSets to solve problems. This makes some programs simpler.


Lazy. This type implements the lazy instantiation pattern. We look into the concept of thunks, as described in a programming textbook. The Lazy class has no relation to the Sleep method.


ReadOnlyCollection. We use this to wrap other collections. It makes those objects read-only—they can no longer be modified. It is not often useful in my experience.


Generics. Many of the most powerful (and fastest) collections are found in System.Collections.Generic. These are generic types. The syntax is strange yet powerful.

Generic Class

Other lists. In lists, one element is stored after the other. The List generic type is often the best implementation available for its purpose. But there are other versions of lists.


Other tables. There are many versions of lookup data structures. Usually a Dictionary is the best option. But sometimes custom features are needed.


Custom types. We can develop custom types based on existing types. This MultiMap is not a great implementation, but I refuse to delete it. We create a Dictionary of Lists.


Performance. Many collections can be optimized with a capacity. This allocates extra initial memory. Often, choosing the best collection is the most effective optimization.

CapacityRemove Element

Collections are essential. They are an additional layer of abstraction. The C# language provides the features to create custom collections. But in practice this is rarely needed.