Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "Set" data structure in .Net?

People also ask

What is a set data structure?

A set is a data structure that stores unique elements of the same type in a sorted order. Each value is a key, which means that we access each value using the value itself. With arrays, on the other hand, we access each value by its position in the container (the index). Accordingly, each value in a set must be unique.

Is C# good for data structures and algorithms?

In this Data Structures and Algorithms Through C# In Depth course, C# programs are used for implementing various concepts, but you can easily code them in any other programming language like C++, Java or Python.

What is the use of set in C#?

The set keyword defines an accessor method in a property or indexer that assigns a value to the property or the indexer element. For more information and examples, see Properties, Auto-Implemented Properties, and Indexers. The following example defines both a get and a set accessor for a property named Seconds .


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


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!


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


Have you checked out the HashSet in 3.5?


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.


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.