HashSet C# with Example



HashSet C# with Example

This is a collection of unique items, with O(1) lookup. 
HashSet validStoryPointValues = new HashSet() { 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 validStoryPointValues = new List() { 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. 

0 Comment's

Comment Form

Submit Comment