views:

967

answers:

6

Ideally, I'm looking for a templated logical Set class. It would have all of the standard set operations such as Union, Intersection, Etc., and collapse duplicated items.

I ended up creating my own set class based on the C# Dictionary<>- just using the Keys.

+12  A: 

HashSet is about the closest you'll get, I think.

Matt Hamilton
+9  A: 

No, there is not one natively in the framework. There is an open source implementation that most projects use, (i.e. nHibernate) called Iesi.Collections. Here's a CodeProject article about it:

http://www.codeproject.com/KB/recipes/sets.aspx

Dale Ragan
+1  A: 

Matt, +1. That sounds like exactly what he asked for. Looks like that was added long after the codeproject article was written.

Derek Park
+5  A: 

I don't think c# has anything built in, but I know there are a couple of implementations floating around on the net. There are also some good articles around on this sort of thing:

This is part 6 of a series on efficiently representing data structure. This part focuses on representing sets in C#.

An implementation of a set collection
An implementation of a set class
Yet another implementation of a set class

And finally...

I've actually used this library myself as the basis of a set implementation that I did a year or so ago.

lomaxx
+12  A: 

The best set implementation I have seen is part of the wonderful Wintellect's Power Collections: http://www.codeplex.com/PowerCollections.

The set implementation can be found here:
http://www.codeplex.com/PowerCollections/SourceControl/FileView.aspx?itemId=101886&amp;changeSetId=6259
It has all the expected set operations (union, intersect, etc).

Hope this helps!

Brad Leach
+6  A: 

Have you checked out the HashSet in 3.5?

Stephen franklin