devforall devforall - 1 year ago 160
C# Question

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

How do I convert an array to a hash set ?

string[] BlockedList = BlockList.Split(new char[] { ';' },

I need to convert this list to a

Answer Source

You do not specify what type BlockedList is, so I will assume it is something that derives from IList (if meant to say String where you wrote BlockList then it would be a string array which derives from IList).

HashSet has a constructor that takes an IEnumerable, so you need merely pass the list into this constructor, as IList derives from IEnumerable.

var set = new HashSet(BlockedList);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download