Page 95 - CSharp/C#
P. 95
Chapter 8: An overview of c# collections
Examples
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.
SortedSet
// 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
T[ ] (Array of T)
// create an array with 2 elements
var myArray = new [] { "one", "two" };
// enumerate through the array
foreach(var item in myArray)
{
Console.WriteLine(item);
}
https://riptutorial.com/ 41

