Overriding GetHashCode()

前端 未结 3 687
臣服心动
臣服心动 2021-01-17 08:19

In this article, Jon Skeet mentioned that he usually uses this kind of algorithm for overriding GetHashCode().

public override int GetHashCode()
{
          


        
3条回答
  •  独厮守ぢ
    2021-01-17 08:40

    Please note that your GetHashCode must go hand in hand with your Equals method. And if you can just use reference equality (when you'd never have two different instances of your class that can be equal) then you can safely use Equals and GetHashCode that are inherited from Object. This would work much better than simply return 1 from GetHashCode.

提交回复
热议问题