34

Whats the best way of doing it?

var set2 = new HashSet<reference_type>();

Traverse the set with a foreach like this.

foreach (var n in set)
    set2.Add(n);

Or use something like union like this.

set2 = set.UnionWith(set); // all the elements

3 Answers 3

55

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.

1
  • 3
    Just an update: LINQ now has a ToHashSet extension method. Commented Mar 24, 2020 at 16:47
19

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.

6

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.