I guess another way to phrase this would be "Is there a class like List<>
in C#, but optimized for checking whether a particular value is present?" I'm sure for a small set of values List<>.Contains
would probably be fine, but what if I have a set of thousands or millions of values and wanted to find out whether a certain value was in it?
I've implemented this kind of thing in the past by creating a Dictionary<object, int>
and setting the value to 0 for every key, but this feels really clunky. And now there's Stack Overflow, where my stupid question can be transformed into education for thousands (dozens, even). So here it is!
I'm not even sure what such a class would be called, other than maybe Set, so obviously searches on the topic have been... challenging :)
Generally, the C standard library does not include a built-in dictionary data structure, but the POSIX standard specifies hash table management routines that can be utilized to implement dictionary functionality.
The Dictionary<TKey, TValue> Class in C# is a collection of Keys and Values. It is a generic collection class in the System. Collections. Generic namespace. The Dictionary <TKey, TValue> generic class provides a mapping from a set of keys to a set of values.
It's a dictionary of dictionaries, so you have 2 keys to access each object, the key for the main dictionary to get you the required sub dictionary, and then the second key for the sub dictionary to get you the required item.
Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false.
Try using the HashSet<T>
class.
Edit: I spent a long long time doing exactly what you did until I just stumbled on this class while reading a blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With