Mark Kanof
Mark Kanof

Reputation: 1446

Data structure like Dictionary but without a value in .NET

Is there any data structure in .NET that is like a Dictionary<TKey,TValue> but that only has a key and doesn't have a value? I basically want a list of integers that I can quickly lookup and see if a certain value is in the list. Granted, for my current use, a List<int> would not cause any performance problem, but it just doesn't seem to fit well with the intent of what my code is doing.

Upvotes: 79

Views: 20102

Answers (4)

Eric J.
Eric J.

Reputation: 150238

If you're not targeting .NET 3.5, Power Collections (open source) also provides a Set implementation.

Upvotes: 1

Paul Sasik
Paul Sasik

Reputation: 81567

If 3.5 is not an option you could do something like Dictionary<int, int> and simply ignore the value. I've done this in 2.0 and I tend to set the value to the same as the key.

Upvotes: 3

Meta-Knight
Meta-Knight

Reputation: 17875

Yes, it's called a HashSet<T>, and available in version 3.5 of the .NET framework. If you use .NET version 2.0, you can use a Dictionary and set values to null.

Upvotes: 109

Gregoire
Gregoire

Reputation: 24882

or use a SortedList where values have to be unique

Upvotes: 0

Related Questions