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

C#.Net

C# Problem Overview


How do I convert an array to a hash set ?

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

I need to convert this list to a hashset.

C# Solutions


Solution 1 - C#

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);

Solution 2 - C#

I'm assuming BlockList is a string (hence the call to Split) which returns a string array.

Just pass the array (which implements IEnumerable) to the constructor of the HashSet:

var hashSet = new HashSet<string>(BlockedList);

Solution 3 - C#

Here is an extension method that will generate a HashSet from any IEnumerable:

public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
	return new HashSet<T>(source);
}

To use it with your example above:

var hashSet = BlockedList.ToHashSet();

Solution 4 - C#

Update 2017 - For .Net Framework 4.7.1+ and .Net Core 2.0+

There is now a built-in ToHashSet method:

var hashSet = BlockedList.ToHashSet();

Solution 5 - C#

Missed new keyword on extension example....

  public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
    {
        return new HashSet<T>(source);
    }

Solution 6 - C#

To take one step further, the following one-liner demonstrates how you can convert a literal string array to a HashSet, so that you do NOT have to define an intermediate variable SomethingList.

var directions = new HashSet<string>(new [] {"east", "west", "north", "south"});

Solution 7 - C#

enter image description here

List<int> a1 = new List<int> { 1, 2 };
List<int> b1 = new List<int> { 2, 3 };
List<int> a2 = new List<int> { 1, 2, 3 };
List<int> b2 = new List<int> { 1, 2, 3 };
List<int> a3 = new List<int> { 2, 3 };
List<int> b3 = new List<int> { 1, 2, 3 };

List<int> a4 = new List<int> { 1, 2, 3 };
List<int> b4 = new List<int> { 2, 3 };
List<int> a5 = new List<int> { 1, 2 };
List<int> b5 = new List<int> { };
List<int> a6 = new List<int> { };
List<int> b6 = new List<int> { 1, 2 };
List<int> a7 = new List<int> { };
List<int> b7 = new List<int> { };

HashSet<int> first = new HashSet<int>(a1);
HashSet<int> second = new HashSet<int>(b1);
first.Overlaps(second);

first = new HashSet<int>(a2);
second = new HashSet<int>(b2);
first.Overlaps(second);

first = new HashSet<int>(a3);
second = new HashSet<int>(b3);
first.Overlaps(second);

first = new HashSet<int>(a4);
second = new HashSet<int>(b4);
first.Overlaps(second);

first = new HashSet<int>(a5);
second = new HashSet<int>(b5);
first.Overlaps(second);

first = new HashSet<int>(a6);
second = new HashSet<int>(b6);
first.Overlaps(second);

first = new HashSet<int>(a7);
second = new HashSet<int>(b7);
first.SetEquals(second);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondevforallView Question on Stackoverflow
Solution 1 - C#Paul RuaneView Answer on Stackoverflow
Solution 2 - C#Justin NiessnerView Answer on Stackoverflow
Solution 3 - C#Jake PearsonView Answer on Stackoverflow
Solution 4 - C#Stas BoyarincevView Answer on Stackoverflow
Solution 5 - C#CountZeroView Answer on Stackoverflow
Solution 6 - C#RayLuoView Answer on Stackoverflow
Solution 7 - C#HemendrView Answer on Stackoverflow