Tutorial by Examples

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: Lis...
// create an empty set var mySet = new SortedSet<int>(); // add something // note that we add 2 before we add 1 mySet.Add(2); mySet.Add(1); // enumerate through the set foreach(var item in mySet) { Console.WriteLine(item); } // output: // 1 // 2
// create an array with 2 elements var myArray = new [] { "one", "two" }; // enumerate through the array foreach(var item in myArray) { Console.WriteLine(item); } // output: // one // two // exchange the element on the first position // note that all collecti...
List<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); // 6 list.RemoveAt(3); Console.WriteLine(list.Count); // 5 ...
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 data Console.Wri...
// Initialize a stack object of integers var stack = new Stack<int>(); // add some data stack.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 elemen...
// initialize a LinkedList of integers LinkedList 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...
// Initalize a new queue of integers var queue = new Queue<int>(); // Add some data queue.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 ele...

Page 1 of 1