Shallow copy of a hashset
Use the constructor:
HashSet<type> set2 = new HashSet<type>(set1);
Personally I wish LINQ to Objects had a ToHashSet
extension method as it does for List
and Dictionary
. It's easy to create your own of course:
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return new HashSet<T>(source);
}
(With an other overload for a custom equality comparer.)
This makes it easy to create sets of an anonymous type.
This is probably the easiest and best:
HashSet<int> s = new HashSet<int>{1,2,3};
HashSet<int> t = new HashSet<int>(s);
From the MSDN docs:
HashSet<T>(IEnumerable<T> collection)
Initializes a new instance of the HashSet class that uses the default equality comparer for the set type, contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied.
Best is subjective, but I'd do it as follows:
set2 = new HashSet<type>(set);
Or even better:
set2 = new HashSet<type>(set, set.Comparer);
which ensures you're using the same equality comparer as the original HashSet. For example if the original was a case-insensitive HashSet<string>
, your new one will also be case-insensitive.