60

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.

Chris
  • 6,761
  • 6
  • 52
  • 67
  • @d03boy: Well it has HashSet now, but after using it a bit I think the interface really sucks. – Skurmedel Mar 01 '10 at 11:49
  • 5
    .NET 4 has an ISet interface along with two implementations, HashSet and SortedSet – Eric Hauser Jun 29 '10 at 04:09
  • See [this question](https://stackoverflow.com/questions/47688779/set-class-with-mathematical-set-equality-by-default) for a simple implementation of a set. – dharmatech May 10 '18 at 01:50
  • Possible duplicate of [C# Set collection?](https://stackoverflow.com/questions/183685/c-sharp-set-collection) – dharmatech May 10 '18 at 04:06

6 Answers6

52

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

spender
  • 117,338
  • 33
  • 229
  • 351
Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • 1
    Matt, +1. That sounds like exactly what he asked for. Looks like that was added long after the codeproject article was written. – Derek Park Aug 13 '08 at 23:16
15

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&changeSetId=6259
It has all the expected set operations (union, intersect, etc).

Hope this helps!

Brad Leach
  • 16,857
  • 17
  • 72
  • 88
10

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
  • 18,202
  • 3
  • 54
  • 70
10

Have you checked out the HashSet in 3.5?

Powerlord
  • 87,612
  • 17
  • 125
  • 175
Stephen franklin
  • 303
  • 1
  • 3
  • 11
6

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
  • 113,627
  • 57
  • 144
  • 179
2

Here's a simple implementation:

public sealed class MathSet<T> : HashSet<T>, IEquatable<MathSet<T>>
{
    public override int GetHashCode() => this.Select(elt => elt.GetHashCode()).Sum().GetHashCode();

    public bool Equals(MathSet<T> obj) => SetEquals(obj);

    public override bool Equals(object obj) => Equals(obj as MathSet<T>);

    public static bool operator ==(MathSet<T> a, MathSet<T> b) =>
        ReferenceEquals(a, null) ? ReferenceEquals(b, null) : a.Equals(b);

    public static bool operator !=(MathSet<T> a, MathSet<T> b) => !(a == b);
}

Example usage:

var a = new MathSet<int> { 1, 2, 3 };
var b = new MathSet<int> { 3, 2, 1 };

var c = a.Equals(b);                        // true

var d = new MathSet<MathSet<int>> { a, b }; // contains one element

var e = a == b;                             // true

See this question for why this approach was considered over HashSet.

dharmatech
  • 8,979
  • 8
  • 42
  • 88