[ACCEPTED]-Why have HashSet but not Set in C#?-hashset

Accepted answer
Score: 19

(Your original question about set has been 38 answered. IIRC, "set" is the word with the 37 most different meanings in the English language... obviously 36 this has an impact in computing too.)

I think 35 it's fine to have HashSet<T> with that name, but I'd 34 certainly welcome an ISet<T> interface. Given that 33 HashSet<T> only arrived in .NET 3.5 (which in itself 32 was surprising) I suspect we may eventually 31 get a more complete collection of set-based 30 types. In particular, the equivalent of 29 Java's LinkedHashSet, which maintains insertion order, would 28 be useful in some cases.

To be fair, the 27 ICollection<T> interface actually covers most of what 26 you'd want in ISet<T>, so maybe that isn't required. However, you 25 could argue that the core purpose of a set 24 (which is mostly about containment, and 23 only tangentially about being able to iterate 22 over the elements) isn't quite the same 21 as a collection. It's tricky. In fact, a 20 truly mathematical set may not be iterable 19 or countable - for instance, you could have 18 "the set of real numbers between 1 and 2." If 17 you had an arbitrary-precision numeric type, the 16 count would be infinite and iterating over 15 it wouldn't make any sense.

Likewise the 14 idea of "adding" to a set doesn't always 13 make sense. Mutability is a tricky business 12 when naming collections :(

EDIT: Okay, responding 11 to the comment: the keyword set is in no way 10 a legacy to do with Visual Basic. It's the 9 operation which sets the value of a property, vs 8 get which retrieves the operation. This has nothing 7 to do with the idea of a set as an operation.

Imagine 6 that instead the keywords were actually 5 fetch and assign, e.g.

// Not real code!
public int Foo
        return fooField;
        fooField = value;

Is the purpose clear there? Now 4 the real equivalent of that in C# is just

public int Foo
        return fooField;
        fooField = value;

So 3 if you write:

x = y.Foo;

that will use the get part of 2 the property. If you write:

y.Foo = x;

that will use 1 the set part.

Is that any clearer?

Score: 6

The only reason for this seems lack of resources 5 to implement this ideally in .NET 3.5.

.NET 4 4.0 will include ISet, as well as its new implementation 3 in addition to HashSet - SortedSet. Check out the provided 2 links to MSDN library - they're already 1 available in .NET 4.0 beta1.

Score: 5

There is no Set<T>. This BCL team Blog post has lot's of details 4 on HashSet including a not entirely conclusive 3 discussion on including hash in the name. I 2 suspect not everyone on the BCL team liked 1 the decision to use the name HashSet<T>.

Score: 4

set is a C# language keyword that has been 13 around since version 1.0. Is is used to 12 define the value-assigning part of a property 11 (and get is used to implement the value-reading 10 part of a property). In this context you 9 should understand the word 'set' as a verb, as 8 in setting a value.

HashSet<T> is a particular implmentation 7 of the mathematical concept of a Set. It 6 was first introduced in .NET 3.5. This blog 5 post by the BCL Team explains more about 4 the reasoning behind it, as well as some 3 clues to why the name is HashSet<T> and not just Set<T>: http://blogs.msdn.com/bclteam/archive/2006/11/09/introducing-hashset-t-kim-hamilton.aspx.

In 2 the case of HashSet<T> you should understand the word 1 'set' as a noun.

Score: 3

Set is a reserved keyword in VB.NET (it's 4 the equivalent to set in C#). VB.NET can 3 use classes/methods/etc with the same name 2 as keywords but they have to be written 1 between square brackets, which it's ugly:

Imports Wintellect.PowerCollections 'PowerCollections contains a class called Set'
Public Class Test
    Private _myValue As Integer  

    Public Property MyValue() As Integer
            Return _myValue
        End Get
        Set ' Set as keyword'
            _myValue = value
        End Set
    End Property

    Public Function X As [Set](Of Integer)
        Dim a As New [Set](Of Integer) ' Set as class'
        Return a
    End Function

End Class
Score: 2

Ah right I understand your question now
Not 31 sure I can 100% see the need for an ISet<T>.
I 30 guess the question is which do you see as 29 essential behaviour for a set?
Is it Add,Remove, Contains 28 etc. If so then ICollection<T> already provides an interface 27 for that.
If it's set operations such as 26 Union, Intersect, etc then is that something 25 you'd consider generic enough to abstract 24 out to a contract style enforcement?

I have 23 to say I don't know the right answer to 22 this one - I think it's open to debate and 21 I suspect the BCL team may end up putting 20 something like this in a future version 19 but that's up to them. I personally don't 18 see it as massive missing piece of functionality

Original Post

The 17 BCL doesn't have a Set collection at all, at 16 least not as far as I know.
There a few 15 3rd party Set libs out there like Iesi.Collections
HashSet<T> was 14 introduced in .NET 3.5 to create a fast 13 set collection i.e where you want a collection 12 with no duplicates. It also has typical 11 set operations such as Union and Join. Check 10 out this link from BCL team on HashSet

You'd typically 9 use it where previously you had to use List<T> and 8 check for duplicates when adding.
Adding 7 items to a HashSet<T> can also be significantly faster than List

Some further 6 details:
Another nice feature of HashSet 5 is that it doesn't throw an exception if 4 you try and add a duplicate it just fails 3 to add the duplicate entry which saves you 2 having to put lots of try.catch blocks around 1 every add - nice :)

Score: 0

I'm pretty sure there's no Set<T> class in the 9 BCL, at least in .NET 3.5 (and not .NET 8 4.0 either it seems). What would you expect 7 is the need for such a class, anyway?

HashSet<T> is 6 itself just an ordinary set data structure 5 that uses hash codes (the GetHashCode method of an 4 object) to compare elements. This is simply 3 an efficient way of implementing a set type. (Other 2 methods for checking equality would likely 1 have lower performance.)

More Related questions