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

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



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Randy Hall