Get the element from hashset where hashset.Count==1

Viewed 12594

How to get the element from hashset that is known to contain exactly 1 element? (without iterating it)

2 Answers

I had a HashSet<object> that for some reason couldn't be accessed with [0] or .First().

Though technically iterating, I'm leaving this here just in case someone else with my issue runs into this post.

foreach (var i in myHash){
    object o = i;
    break;
}

Simply start to iterate, then immediately break the iteration.

Related