Cristian Diaconescu
Cristian Diaconescu

Reputation: 35651

.NET Generic Set?

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

Answers (3)

Cristian Diaconescu
Cristian Diaconescu

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:

  • HashSet<T>:

    ...provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order

  • SortedSet<T>:

    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

Jennifer
Jennifer

Reputation: 5218

I use the Iesi.Collections. namespace that comes with NHibernate (docs here) - maybe worth considering if you are in .NET < 3.5

Upvotes: 3

leppie
leppie

Reputation: 117250

HashSet<T> in .NET 3.5

Upvotes: 20

Related Questions