views:

20278

answers:

8
+37  Q: 

C# Set collection?

Does anyone know if there is a good equivelent to Java's Set collection in C#.

It's one of the few things I miss from having moved to C#.

I am aware that you can have a "pretend" set using a Dictionary or Hashtable and only bothering to populate the keys. But it's not very elegant.

It's wierd every other way I turn in C# somone seems to have thought of a solution and built it into the framework, except for sets...

+13  A: 

Try HashSet

http://msdn.microsoft.com/en-us/library/bb495294.aspx

Leahn Novash
Unfortunately, HashSets weren't added until just recently. If you're working in an older version of the framework, you're going to have to stick with your munged Dictionary<> or Hashtable.
Greg D
+44  A: 

If you're using .NET 3.5, you can use HashSet<T>. It's true that .NET doesn't cater for sets as well as Java does though.

The Wintellect PowerCollections may help too.

Jon Skeet
I hadn't noticed that being added. Microsoft have answered my prayers...
Omar Kooheji
does anyone know why it's called HashSet and not just Set?
Wouter
I suspect that Set is a keyword in some languages, which could cause issues.
Jon Skeet
`Set` is a keyword in VB.
Pavel Minaev
`set` is also a keyword in C#
Manish Sinha
@Manish: No it's not. See section 2.4.3 of the C# 3 spec. It only has special meaning for properties.
Jon Skeet
@JonThanks for the enlightenment. I always thought it was a keyword. Downloaded the C# 3 spec.
Manish Sinha
The reason for calling it HashSet, instead of just Set, is the same as in Java- "Set" describes an interface, whereas "HashSet" describes an implementation- specifically, this is a Set backed by a Hash Map. In this way, we know (or should strongly expect) that insert and access should take O(1) access time, vs a "LinkedListSet" which would lead us to expect insert and access to take O(n) time.
David Souther
+5  A: 

I use Iesi.Collections http://www.codeproject.com/KB/recipes/sets.aspx

It's used in lot of OSS projects, I first came across it in NHibernate

Chris Canal
+5  A: 

Have a look at PowerCollections over at CodePlex. Apart from Set and OrderedSet it has a few other usefull collection types such as Deque, MultiDictionary, Bag, OrderedBag, OrderedDictionary and OrderedMultiDictionary.

For more collections, there is also the C5 Generic Collection Library.

dimitrisp
A: 

You could implement your own workable set implementation in a couple of hours. I used this when I had to do it (sorry, I don't have the code handy): http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html

cciotti
A: 

I had this same question for .NET, but used via Powershell 1.0

HashSet would be great, but it is not available in 2.0, but then I found this site :

link text

"a","b","b","c","a" | Select-Object -Unique

returns a b c

So while not answering the C# question, hope it may be useful to a PowerShell user.

bamboowave
A: 

You can apply set operations to any two IEnumerables using the LINQ extension methods, which sometimes is sufficient: IEnumerable extension methods.

bbache
If you have LINQ extension methods, you have 3.5, and therefore `HashSet<T>`. It's also worth noting that `IEnumerable` "set-like" methods are rather inefficient compared to a proper set implementation.
Pavel Minaev
+3  A: 

I use a wrapper around a Dictionary<T, object>, storing nulls in the values. This gives O(1) add, lookup and remove on the keys, and to all intents and purposes acts like a set.

thecoop