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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the constructor:
Personally I wish LINQ to Objects had a
ToHashSetextension method as it does forListandDictionary. It’s easy to create your own of course:(With an other overload for a custom equality comparer.)
This makes it easy to create sets of an anonymous type.