How to retrieve actual item from HashSet?

前端 未结 11 520
花落未央
花落未央 2020-12-02 17:55

I\'ve read this question about why it is not possible, but haven\'t found a solution to the problem.

I would like to retrieve an item from a .NET HashSet. I

11条回答
  •  -上瘾入骨i
    2020-12-02 18:24

    This method has been added to .NET Framework 4.7.2 (and .NET Core 2.0 before it); see HashSet.TryGetValue. Citing the source:

    /// 
    /// Searches the set for a given value and returns the equal value it finds, if any.
    /// 
    /// The value to search for.
    /// 
    /// 
    /// The value from the set that the search found, or the default value
    /// of  when the search yielded no match.
    /// A value indicating whether the search was successful.
    /// 
    /// This can be useful when you want to reuse a previously stored reference instead of 
    /// a newly constructed one (so that more sharing of references can occur) or to look up
    /// a value that has more complete data than the value you currently have, although their
    /// comparer functions indicate they are equal.
    /// 
    public bool TryGetValue(T equalValue, out T actualValue)
    

提交回复
热议问题