C# | An overview of c# collections
HashSet
Section titled “HashSet”This is a collection of unique items, with O(1) lookup.
HashSet<int> validStoryPointValues = new HashSet<int>() { 1, 2, 3, 5, 8, 13, 21 };bool containsEight = validStoryPointValues.Contains(8); // O(1)By way of comparison, doing a Contains on a List yields poorer performance:
List<int> validStoryPointValues = new List<int>() { 1, 2, 3, 5, 8, 13, 21 };bool containsEight = validStoryPointValues.Contains(8); // O(n)HashSet.Contains uses a hash table, so that lookups are extremely fast, regardless of the number of items in the collection.
Dictionary<TKey, TValue>
Section titled “Dictionary<TKey, TValue>”Dictionary<TKey, TValue> is a map. For a given key there can be one value in the dictionary.
using System.Collections.Generic;
var people = new Dictionary<string, int>{ { "John", 30 }, {"Mary", 35}, {"Jack", 40}};
// Reading dataConsole.WriteLine(people["John"]); // 30Console.WriteLine(people["George"]); // throws KeyNotFoundException
int age;if (people.TryGetValue("Mary", out age)){ Console.WriteLine(age); // 35}
// Adding and changing datapeople["John"] = 40; // Overwriting values this way is okpeople.Add("John", 40); // Throws ArgumentException since "John" already exists
// Iterating through contentsforeach(KeyValuePair<string, int> person in people){ Console.WriteLine("Name={0}, Age={1}", person.Key, person.Value);}
foreach(string name in people.Keys){ Console.WriteLine("Name={0}", name);}
foreach(int age in people.Values){ Console.WriteLine("Age={0}", age);}Duplicate key when using collection initialization
Section titled “Duplicate key when using collection initialization”var people = new Dictionary<string, int>{ { "John", 30 }, {"Mary", 35}, {"Jack", 40}, {"Jack", 40}}; // throws ArgumentException since "Jack" already existsSortedSet
Section titled “SortedSet”// create an empty setvar mySet = new SortedSet<int>();
// add something// note that we add 2 before we add 1mySet.Add(2);mySet.Add(1);
// enumerate through the setforeach(var item in mySet){ Console.WriteLine(item);}
// output:// 1// 2T[ ] (Array of T)
Section titled “T[ ] (Array of T)”// create an array with 2 elementsvar myArray = new [] { "one", "two" };
// enumerate through the arrayforeach(var item in myArray){ Console.WriteLine(item);}
// output:// one// two
// exchange the element on the first position// note that all collections start with the index 0myArray[0] = "something else";
// enumerate through the array againforeach(var item in myArray){ Console.WriteLine(item);}
// output:// something else// twoList<T> is a list of a given type. Items can be added, inserted, removed and addressed by index.
using System.Collections.Generic;
var list = new List<int>() { 1, 2, 3, 4, 5 };list.Add(6);Console.WriteLine(list.Count); // 6list.RemoveAt(3);Console.WriteLine(list.Count); // 5Console.WriteLine(list[3]); // 5List<T> can be thought of as an array that you can resize. Enumerating over the collection in order is quick, as is access to individual elements via their index. To access elements based on some aspect of their value, or some other key, a Dictionary<T> will provide faster lookup.
// Initialize a stack object of integersvar stack = new Stack<int>();
// add some datastack.Push(3);stack.Push(5);stack.Push(8);
// elements are stored with "first in, last out" order.// stack from top to bottom is: 8, 5, 3
// We can use peek to see the top element of the stack.Console.WriteLine(stack.Peek()); // prints 8
// Pop removes the top element of the stack and returns it.Console.WriteLine(stack.Pop()); // prints 8Console.WriteLine(stack.Pop()); // prints 5Console.WriteLine(stack.Pop()); // prints 3LinkedList
Section titled “LinkedList”// initialize a LinkedList of integersLinkedList list = new LinkedList<int>();
// add some numbers to our list.list.AddLast(3);list.AddLast(5);list.AddLast(8);
// the list currently is 3, 5, 8
list.AddFirst(2);// the list now is 2, 3, 5, 8
list.RemoveFirst();// the list is now 3, 5, 8
list.RemoveLast();// the list is now 3, 5Note that LinkedList<T> represents the doubly linked list. So, it’s simply collection of nodes and each node contains an element of type T. Each node is linked to the preceding node and the following node.
// Initalize a new queue of integersvar queue = new Queue<int>();
// Add some dataqueue.Enqueue(6);queue.Enqueue(4);queue.Enqueue(9);
// Elements in a queue are stored in "first in, first out" order.// The queue from first to last is: 6, 4, 9
// View the next element in the queue, without removing it.Console.WriteLine(queue.Peek()); // prints 6
// Removes the first element in the queue, and returns it.Console.WriteLine(queue.Dequeue()); // prints 6Console.WriteLine(queue.Dequeue()); // prints 4Console.WriteLine(queue.Dequeue()); // prints 9Thread safe heads up! Use [ConcurrentQueue](https://msdn.microsoft.com/en-us/library/dd267265) in multi-thread environments.