C#:字典值到hashset转换
请建议将Dictionary
转换为Hashset
的最短路径
IEnumerables是否内置ToHashset() LINQ扩展?
先感谢您!
var yourSet = new HashSet(yourDictionary.Values);
或者,如果您愿意,可以使用自己的简单扩展方法来处理类型推理。 然后你不需要显式指定HashSet
的T
HashSet
:
var yourSet = yourDictionary.Values.ToHashSet(); // ... public static class EnumerableExtensions { public static HashSet ToHashSet (this IEnumerable source) { return source.ToHashSet (null); } public static HashSet ToHashSet ( this IEnumerable source, IEqualityComparer comparer) { if (source == null) throw new ArgumentNullException("source"); return new HashSet (source, comparer); } }
new HashSet