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. 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 excellent when used in loops.List
Dictionary. This is an implementation of a hash table: an extremely efficient way to store keys for lookup.
Dictionary is fast,
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
Hashtable. This is a lookup data structure. It uses a hash code to quickly find elements. Dictionary is usually more appropriate for programs.Hashtable
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
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. 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 is a useful data type.
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.
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
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. 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
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
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
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
Other tables. There are many versions of lookup data structures. Usually a Dictionary is the best option. But sometimes custom features are needed.ListDictionaryHybridDictionarySortedDictionaryStringDictionaryNameValueCollectionDictionaryEntryMultiMap
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.