Convert an array to a HashSet in .NET
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# .net
add a comment |
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# .net
What kind of list/array is this? What does it contain?
– Bernard
Nov 11 '10 at 16:29
Calling it BlockList is very misleading. I'd suggest BlockNames.
– Hans Passant
Nov 11 '10 at 17:16
possible duplicate of How to convert linq results to HashSet or HashedSet
– nawfal
Jun 12 '13 at 17:42
add a comment |
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# .net
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# .net
c# .net
edited Sep 13 '12 at 9:14
Mahmoud Gamal
64.5k14107136
64.5k14107136
asked Nov 11 '10 at 16:25
devforall
2,602113042
2,602113042
What kind of list/array is this? What does it contain?
– Bernard
Nov 11 '10 at 16:29
Calling it BlockList is very misleading. I'd suggest BlockNames.
– Hans Passant
Nov 11 '10 at 17:16
possible duplicate of How to convert linq results to HashSet or HashedSet
– nawfal
Jun 12 '13 at 17:42
add a comment |
What kind of list/array is this? What does it contain?
– Bernard
Nov 11 '10 at 16:29
Calling it BlockList is very misleading. I'd suggest BlockNames.
– Hans Passant
Nov 11 '10 at 17:16
possible duplicate of How to convert linq results to HashSet or HashedSet
– nawfal
Jun 12 '13 at 17:42
What kind of list/array is this? What does it contain?
– Bernard
Nov 11 '10 at 16:29
What kind of list/array is this? What does it contain?
– Bernard
Nov 11 '10 at 16:29
Calling it BlockList is very misleading. I'd suggest BlockNames.
– Hans Passant
Nov 11 '10 at 17:16
Calling it BlockList is very misleading. I'd suggest BlockNames.
– Hans Passant
Nov 11 '10 at 17:16
possible duplicate of How to convert linq results to HashSet or HashedSet
– nawfal
Jun 12 '13 at 17:42
possible duplicate of How to convert linq results to HashSet or HashedSet
– nawfal
Jun 12 '13 at 17:42
add a comment |
7 Answers
7
active
oldest
votes
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);
3
CallingSpliton this mystery type, with a parameter of char array andStringSplitOptionskinda indicates that BlockedList is a string.
– Jamiec
Nov 11 '10 at 16:30
as much as I hate making assumptions, looking at the.Splitmethod andStringSplitOptions, I would have to assumeStringarray.
– IAbstract
Nov 11 '10 at 16:32
add a comment |
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);
add a comment |
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();
add a comment |
Missed new keyword on extension example....
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
add a comment |
Begining with .Net Framework 4.7.1 and .Net Core 2.0 there is built-in ToHashSet Method
using System.Linq;
var hashSet = BlockedList.ToHashSet();
add a comment |
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"});
add a comment |

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);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4156626%2fconvert-an-array-to-a-hashsett-in-net%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
3
CallingSpliton this mystery type, with a parameter of char array andStringSplitOptionskinda indicates that BlockedList is a string.
– Jamiec
Nov 11 '10 at 16:30
as much as I hate making assumptions, looking at the.Splitmethod andStringSplitOptions, I would have to assumeStringarray.
– IAbstract
Nov 11 '10 at 16:32
add a comment |
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);
3
CallingSpliton this mystery type, with a parameter of char array andStringSplitOptionskinda indicates that BlockedList is a string.
– Jamiec
Nov 11 '10 at 16:30
as much as I hate making assumptions, looking at the.Splitmethod andStringSplitOptions, I would have to assumeStringarray.
– IAbstract
Nov 11 '10 at 16:32
add a comment |
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);
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);
answered Nov 11 '10 at 16:27
Paul Ruane
27.8k105273
27.8k105273
3
CallingSpliton this mystery type, with a parameter of char array andStringSplitOptionskinda indicates that BlockedList is a string.
– Jamiec
Nov 11 '10 at 16:30
as much as I hate making assumptions, looking at the.Splitmethod andStringSplitOptions, I would have to assumeStringarray.
– IAbstract
Nov 11 '10 at 16:32
add a comment |
3
CallingSpliton this mystery type, with a parameter of char array andStringSplitOptionskinda indicates that BlockedList is a string.
– Jamiec
Nov 11 '10 at 16:30
as much as I hate making assumptions, looking at the.Splitmethod andStringSplitOptions, I would have to assumeStringarray.
– IAbstract
Nov 11 '10 at 16:32
3
3
Calling
Split on this mystery type, with a parameter of char array and StringSplitOptions kinda indicates that BlockedList is a string.– Jamiec
Nov 11 '10 at 16:30
Calling
Split on this mystery type, with a parameter of char array and StringSplitOptions kinda indicates that BlockedList is a string.– Jamiec
Nov 11 '10 at 16:30
as much as I hate making assumptions, looking at the
.Split method and StringSplitOptions, I would have to assume String array.– IAbstract
Nov 11 '10 at 16:32
as much as I hate making assumptions, looking at the
.Split method and StringSplitOptions, I would have to assume String array.– IAbstract
Nov 11 '10 at 16:32
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 11 '10 at 16:28
Justin Niessner
208k30360490
208k30360490
add a comment |
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
edited Feb 23 '13 at 5:01
Leniel Maccaferri
79.7k33287392
79.7k33287392
answered Nov 11 '10 at 17:17
Jake Pearson
17.6k85988
17.6k85988
add a comment |
add a comment |
Missed new keyword on extension example....
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
add a comment |
Missed new keyword on extension example....
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
add a comment |
Missed new keyword on extension example....
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
Missed new keyword on extension example....
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
answered Dec 9 '10 at 16:05
CountZero
3,28733546
3,28733546
add a comment |
add a comment |
Begining with .Net Framework 4.7.1 and .Net Core 2.0 there is built-in ToHashSet Method
using System.Linq;
var hashSet = BlockedList.ToHashSet();
add a comment |
Begining with .Net Framework 4.7.1 and .Net Core 2.0 there is built-in ToHashSet Method
using System.Linq;
var hashSet = BlockedList.ToHashSet();
add a comment |
Begining with .Net Framework 4.7.1 and .Net Core 2.0 there is built-in ToHashSet Method
using System.Linq;
var hashSet = BlockedList.ToHashSet();
Begining with .Net Framework 4.7.1 and .Net Core 2.0 there is built-in ToHashSet Method
using System.Linq;
var hashSet = BlockedList.ToHashSet();
answered Jun 6 '18 at 12:45
Stas Boyarincev
1,247911
1,247911
add a comment |
add a comment |
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"});
add a comment |
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"});
add a comment |
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"});
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"});
answered Jul 26 '17 at 23:36
RayLuo
6,00223946
6,00223946
add a comment |
add a comment |

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);
add a comment |

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);
add a comment |

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

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);
edited Nov 23 '18 at 1:22
answered Nov 23 '18 at 1:04
Hemendr
18218
18218
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4156626%2fconvert-an-array-to-a-hashsett-in-net%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What kind of list/array is this? What does it contain?
– Bernard
Nov 11 '10 at 16:29
Calling it BlockList is very misleading. I'd suggest BlockNames.
– Hans Passant
Nov 11 '10 at 17:16
possible duplicate of How to convert linq results to HashSet or HashedSet
– nawfal
Jun 12 '13 at 17:42