I need to have an ability to have unique items in a collection.
I was going to use a Dictionary so I could use the ContainsKey method but I thought it would be a waste as I wouldnt use the Value property of the Key/Value pair.
I came across the HashSet<T> which looks very promising. The only thing I can find that I can’t find in the List<T> docs is that HashSet<T> is unordered. I think that is fine, I assume it means its not ordered using a IEqualityComparer. As long as the order in which items are added are in the same index position I think it will be ok as I have to do duplicate checking hence the hashset and then check all entries are sequential.
Is there anything else I have missed comparing the two types?
No, importantly
HashSet<T>doesn’t have any concept of ordering or indexing – a list conceptually has slots 0….n-1, whereas a set is “just a set”.IEqualityComparerisn’t used for ordering anyway – it only talks about equality and hash codes.HashSet<T>isn’t ordered by either an element comparison (as, say,SortedSet<T>is) or insertion order.There is no index position, and when you iterate over a
HashSet<T>there’s no guarantee you’ll get them back in the order in which you added them. If you’re even thinking about ordering,HashSet<T>isn’t what you’re after.Then again, all of this is also true of
Dictionary<TKey, TValue>– you shouldn’t make any assumptions about ordering there, either.