'Proper' collection to use to obtain items in O(1) time in C# .NET?

Something I do often if I'm storing a bunch of string values and I want to be able to find them in O(1) time later is:

foreach (String value in someStringCollection)
    someDictionary.Add(value, String.Empty);

This way, I can comfortably perform constant-time lookups on these string values later on, such as:

if (someDictionary.containsKey(someKey))
    // etc

However, I feel like I'm cheating by making the value String.Empty. Is there a more appropriate .NET Collection I should be using?


If you're using .Net 3.5, try HashSet. If you're not using .Net 3.5, try C5. Otherwise your current method is ok (bool as @leppie suggests is better, or not as @JonSkeet suggests, dun dun dun!).

HashSet<string> stringSet = new HashSet<string>(someStringCollection);

if (stringSet.Contains(someString))

Need Your Help

Skrollr relative top-bottom fires early in chrome

javascript jquery animation scroll

I have several relative positioned divs stacked on top of eachother with a height of 100% set with jQuery on load.

get string with foreach in datagridview

c# datagridview foreach getstring

I create a function with the aim to show the concatenated string to the DataGrid that I made ​​such:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.