Convert a HashSet<T> to an array in .NET

Solution 1:

Use the HashSet<T>.CopyTo method. This method copies the items from the HashSet<T> to an array.

So given a HashSet<String> called stringSet you would do something like this:

String[] stringArray = new String[stringSet.Count];
stringSet.CopyTo(stringArray);

Solution 2:

If you mean System.Collections.Generic.HashSet, it's kind of hard since that class does not exist prior to framework 3.5.

If you mean you're on 3.5, just use ToArray since HashSet implements IEnumerable, e.g.

using System.Linq;
...
HashSet<int> hs = ...
int[] entries = hs.ToArray();

If you have your own HashSet class, it's hard to say.