C# Collections

Array Class Collections File Keyword String .NET ASP.NET Cast Compression Data Delegate Directive Enum Exception If Interface 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 excellent when used in loops.

List
Dictionary

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
Syntax

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 you must cast them.

ArrayList
Four

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 your 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
Queue collection

Stack, queue. 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 these types have clearer interfaces.

StackQueue
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
About part

Generics. Many of the most powerful (and fastest) collections are found in System.Collections.Generic. These are generic types. We can use ReadOnlyCollection to wrap generics.

Generic ClassReadOnlyCollection
About part

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
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.

ListDictionaryHybridDictionarySortedDictionaryStringDictionaryNameValueCollectionDictionaryEntryMultiMap
Framework: NET

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

C#