Reputation: 35651
Is there a generic container implementing the 'set' behaviour in .NET?
I know I could just use a Dictionary<T, Object>
(and possibly add nulls
as values), because its keys act as a set, but I was curious if there's something ready-made.
Upvotes: 11
Views: 4707
Reputation: 35651
Introduced in .NET 3.5: HashSet<T>
(see below).
Introduced in .NET 4.0: ISet<T>
:
Provides the base interface for the abstraction of sets. This interface provides methods for implementing sets, which are collections that have unique elements and specific operations.
The interface has two implementations in the .NET 4+ BCL:
...provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order
Represents a collection of objects that is maintained in sorted order. A SortedSet(Of T) maintains a sorted order as elements are inserted and deleted without affecting performance. Duplicate elements are not allowed.
FWIW: There's also an internal
class TreeSet<T>: SortedSet<T>
under the namespace System.Collections.Generic
.
Its only purpose seems to be the usage in the implementation of SortedDictionary<TKey, TValue>
.
Upvotes: 2