C# Collections

Array Collections File Keyword String .NET Cast Class Compression Data Directive Enum Exception If Interface Lambda LINQ Loop Method Number Process Property Regex Sort StringBuilder Struct Switch Time Windows WPF

Collections: List

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.


Collections: Dictionary, Tuple and Set

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.

List
Squares: grey

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.

Dictionary
ArrayList

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.

ArrayList
Hash codes are computed numbers used for lookups

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

Hashtable
Continue

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.

ConcurrentDictionaryConcurrentBag
Bools: bits represented as zeros

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.

BitArray
Tuple

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.

Tuple
KeyValuePair: Key and Value properties

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

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.

Stack
Queue collection

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.

Queue
Set

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.

HashSetSortedSet
Lazy type

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.

Lazy
Squares: grey

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.

ReadOnlyCollection
Generic

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
List

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.

LinkedListSortedList
Iterator

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

ListDictionaryHybridDictionarySortedDictionaryStringDictionaryNameValueCollectionDictionaryEntry
Squares: grey

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.

MultiMap
Performance

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
Framework: NET

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.

C#