views:

311

answers:

9

One of my biggest issues dealing with a move from Java to .Net is the fact that there isn't a Set interface in .Net. I know there are libraries I could go and download but what is the reason for not having it built in? There are Maps (Dictionary) and Lists but why not a Set?

Edit: I should clarify that not everyone uses .Net 3.5 yet -- so I'm more or less referring to older versions of .Net

+3  A: 

Do you mean a HashSet?

Kamarey
+3  A: 

There's HashSet<T> these days, but sadly no interface of which I'm aware.

Doug McClean
+7  A: 

I think it's simply an omission by the BCL writers. .NET 3.5 has a HashSet class; for earlier versions, I recommend wrapping a Dictionary<T, object>, with nulls in the value field, to replicate O(1) add, remove and lookup time.

thecoop
This is exactly how Java's Set implementations work, by the way.
Michael Borgwardt
+2  A: 

Perhaps the reasoning is that a set is really just a list with a particular implementation detail that restricts the items in it to being distinct. Since the distinctness of the list is in the implementation rather than the interface, an interface is not needed.

As others have mentioned, the FCL has the HashSet<T>.

Andrew Hare
You forget a big difference between lists and sets: a list is ordered while a set isn't.
Meta-Knight
I've noticed that the .Net implementors sometimes take an overly.. academic approach to the platform. Build the basic tools. Everything else is an implementation detail.
Joel Coehoorn
Restricting the elements to being distinct is FAR from an implementation detail. It's very much part of the interface for the data type.
erikkallen
Another big difference in performance: find item by value in List=O(n) and in Set (say in Java or .NET)=O(1).
Kamarey
This answer and these comments, although maybe not entirely correct, seem to actually answer my question the best so far
Joe Philllips
@Meta-Knight - That has nothing to do with the _interface_, only the implementation of it. @erikkallen - I would be interested to know which part of Java's `Set<E>` interface (other than its name) has anything to do with the fact that it is _implemented_ as a distinct sequence of items. @Kamarey - You are comparing two implementations which doesn't have anything to do with an interface. The main reason that Java has the `Set<E>` interface is because Java has more than one implementation that could benefit from it. Since .NET only offers one implementation there was no need...
Andrew Hare
... to create an interface. My point is basically this - there is nothing that precludes you from creating an implementation of `Set<E>` (or `ISet<T>` in .NET 4) that isn't actually a set!
Andrew Hare
+4  A: 

.NET 3.5 has HashSet which does all set operations.

Akash Kava
What I'd specifically like to know is why it wasn't implemented sooner
Joe Philllips
Only the BCL team can answer that. It does seem like a significant omission to me.
thecoop
+2  A: 

Maybe because of educational considerations.

A typical programmer sees a sets as a magical container that just works no matter how many elements are in it.

If there is no explicit set, a programmer is forced to choose from other types and while doing so reflect on the elements count and appropriate data structure to achieve good performance.

Just a wild guess.

Developer Art
+3  A: 

As others have noted, there is a HashSet<T>, which is actually just a set.

The reason it has "hash" in front of it (an implementation detail of the set since it uses hashes to eliminate duplicates) is becase Set is a keyword in VB.NET.

John Rasch
Interesting tidbit
Joe Philllips
+5  A: 

In .NET 4.0 HashSet will be retrofitted to even implement new ISet interface.

alex
+2  A: 

I also moved from Java to .Net recently (due to professional employment) and I must admit that my initial problems have also been on collections.
In the current .Net version (3.5 and speaking about C#) you should orientate yourself on

  • ICollection
  • IList<T>
  • List<T>
  • IDictionary<TKey,TValue>
  • IEnumerable<T>

These are the most commonly used (hope I didn't miss one)

Juri
I also use IEnumerable at points, when I want a generalised enumerable without type information and also to get past .NET 2 generics non-covariance
thecoop