HashSet<T> - Finally a Set Implementation in the .NET Class Libraries

CraigBlog

Syndication

Update: Despite what the documentation says, this class is new to .NET 3.5, and is not present in the .NET 3.0 libraries.
 
Update: Fixed typo - HashSet does not allow duplicate members, just as you'd expect.
 
I was poking around the other day and I noticed System.Collections.Generics.HashSet<T>. Sure enough, it's a collection class with the semantics of a set: there are no duplicates no matter how many times you add an item to the set, it is unordered, and it supports intersection, union, and subset operations.
 
It's part of .NET 3.0 3.5, so it's been around for a little while not quite out yet. I doubt I'm the last (although I'm even more sure I'm not the first :) to hear of it, though. Go check it out, because a set is one of those data structures that gets used all the time. If you've ever instantiated a Dictionary<T, bool> just to keep track of if you've seen something, you know what I mean.
 
Thanks, CLR team!

Posted Sep 25 2007, 09:35 AM by craig-andera

Comments

Pedro Felix wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-25-2007 8:19 AM
I think it is available only on .NET 3.5 and not on .NET 3.0. Am I wrong?
Dave Bettin wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-25-2007 8:21 AM
Hey Craig,

I assume you meant "no duplicates". I hope so.. cause if duplicates are allowed then I might have to go change some code.

Also, this has been in the fx since 3.5 not 3.0.

BTW, I love your REST thinking. I too am not digging WCF's "REST Support". It kind of feels like putting makeup on a sloth.

Dave
Craig wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-25-2007 9:25 AM
Bah - fixed the "no duplicates" typo. Thanks for pointing it out.

As far as 3.0 vs. 3.5, the documentation says it's in both. I haven't verified it.

Dave: I'm having a good time living my version of the REST lifestyle. I've wound up rolling my own framework but it has been pretty easy, and the obvious benefit is total control over what goes out on the wire. Xlinq has proven to be a pretty handy way of working with the content, too.
Daniel Moth wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-26-2007 2:28 AM
Can you point me to the documentation please so we can get this fixed?

It is new in v3.5.
Craig wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-26-2007 3:01 AM
Documentation is here: http://msdn2.microsoft.com/en-us/library/bb359438(VS.90).aspx
Jason Bock wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-26-2007 7:08 AM
For the life of me, I can't find it in 3.0 - I thought this was just a 3.5 addition? I realize the doc ref you gave says 3.0 ... but I think that's a typo :)
Craig wrote re: HashSet&lt;T&gt; - Finally a Set Implementation in the .NET Class Libraries
on 09-26-2007 7:31 AM
Confirmed - it's a doc typo.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?