Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Data Structure Like Dictionary But Without A Value

Is there any data structure in C# that is like a dictionary 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 would not cause any performance problem, but it just doesn't seem to fit well with the intent of what my code is doing.

like image 916
Mark Kanof Avatar asked Sep 01 '09 17:09

Mark Kanof


2 Answers

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.

like image 142
Meta-Knight Avatar answered Sep 18 '22 13:09

Meta-Knight


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.

like image 35
Paul Sasik Avatar answered Sep 21 '22 13:09

Paul Sasik