C#:字典值到哈希集的转换

C#:字典值到哈希集的转换,c#,dictionary,hashset,C#,Dictionary,Hashset,请建议将字典转换为哈希集 IEnumerables是否有内置的ToHashset()LINQ扩展 提前谢谢你 新哈希集(YourDict.Values)这个问题和答案对我来说毫无意义。根据MSDN,哈希集不能包含重复的元素,应该被视为没有值的字典集合。从字典中获取所有值并将它们分配给HashSet给我是没有意义的。不幸的是,KeyCollection对象与HashSet对象不同,尽管我多么希望它是这样。记住这个问题已经有5年了;我同意@ChristopherPainter。重点是什么?就唯一性而

请建议将
字典
转换为
哈希集

IEnumerables是否有内置的ToHashset()LINQ扩展


提前谢谢你

新哈希集(YourDict.Values)

这个问题和答案对我来说毫无意义。根据MSDN,哈希集不能包含重复的元素,应该被视为没有值的字典集合。从字典中获取所有值并将它们分配给HashSet给我是没有意义的。不幸的是,KeyCollection对象与HashSet对象不同,尽管我多么希望它是这样。记住这个问题已经有5年了;我同意@ChristopherPainter。重点是什么?就唯一性而言,更有意义的是:
newhashset(myDictionary.Keys)
var yourSet = new HashSet<TValue>(yourDictionary.Values);
var yourSet = yourDictionary.Values.ToHashSet();

// ...

public static class EnumerableExtensions
{
    public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
    {
        return source.ToHashSet<T>(null);
    }

    public static HashSet<T> ToHashSet<T>(
        this IEnumerable<T> source, IEqualityComparer<T> comparer)
    {
        if (source == null) throw new ArgumentNullException("source");

        return new HashSet<T>(source, comparer);
    }
}