0

Possible Duplicate:
HashSet replacement in C# 2.0

I am in need of a collection that does not allow duplicates. In dotNet v4.0 I would use a HashSet.

What options do I have in dotNet v2.0?

This is the only alternative I can think of for adding without duplicates:

List<Foo> list = new List<Foo>();
if (!list.Contains(newItem)) list.Add(newItem);

But it will be facing much more attemped duplicates than non-duplicates, which makes the O(n) nature of List.Contains less than appealing.

What other collection should I use instead? Would a Dictionary<Foo,JunkValue> be useful?

The collection does not have to be ordered.

Community
  • 1
  • 1
Rotem
  • 21,452
  • 6
  • 62
  • 109
  • Yes, you would use a dictionary. It will throw if you try to add a key/value pair where the key already exists. – Robert Harvey Jan 23 '13 at 19:22
  • @TimSchmelter Thanks my search didn't find that one for some reason. – Rotem Jan 23 '13 at 19:22
  • @Rotem: it was the first hit when i searched for "hashset 2.0". However, note that you can use a `HashSet` in a 2.0 application. http://stackoverflow.com/a/687042/284240 – Tim Schmelter Jan 23 '13 at 19:24

0 Answers0