C# Data Structure Like Dictionary But Without A Value

C#.NetData Structures

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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

Solution 4 - C#

or use a SortedList where values have to be unique

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark KanofView Question on Stackoverflow
Solution 1 - C#Meta-KnightView Answer on Stackoverflow
Solution 2 - C#Paul SasikView Answer on Stackoverflow
Solution 3 - C#Eric J.View Answer on Stackoverflow
Solution 4 - C#GregoireView Answer on Stackoverflow