var yourSet = new HashSet<TValue>(yourDictionary.Values);
或者,如果您愿意,您可以使用自己的简单扩展方法来处理类型推断。那么您就不需要显式地指定
T
的
HashSet<T>
:
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);
}
}