Something like IDictionary <TKey, TValue>, but only for keys (no value required) in .NET?

Is there something in .Net that allows storing / retrieving / contains keys with no values?
I can use Dictionary<string, string>and always store String.Emptyas value, but maybe there is some better solution?

+3
source share
5 answers

You can use a class HashSet<T>, it is designed to store various values ​​in a set.

The main difference between IDictionary{TKey, TValue}(except that it does not store values) is that you can add the same value for HashSet<T>, and if it already exists, it does not throw an exception (when you try to call a method, Addit, unlike the method AddonIDictionary{TKey, TValue} , which will throw ArgumentExceptionif the item already exists in the dictionary.

+11
source

Try Hashset<T>it if you are using .NET 3.5 or higher.

+3
source

.NET 3.5, HashSet. , (, List<> ).

+1

, System.Collections.Generic.HashSet <T>

MSDN:

HashSet <T> . , .

HashSet <T> , . HashSet <T> .

.NET Framework 4, HashSet <T> ISet <T> .

+1

List<T> , , - , HashSet, , , . List<T> .

0

Source: https://habr.com/ru/post/1790737/


All Articles