Tuesday, September 25, 2007

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

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!


  1. I think it is available only on .NET 3.5 and not on .NET 3.0. Am I wrong?

  2. 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.


  3. 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.

  4. Can you point me to the documentation please so we can get this fixed?

    It is new in v3.5.

  5. Documentation is here: http://msdn2.microsoft.com/en-us/library/bb359438(VS.90).aspx

  6. 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 :)

  7. Confirmed - it's a doc typo.