Overriding GetHashCode()

前端 未结 3 653
臣服心动
臣服心动 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:55

    If all your fields are mutable and you have to implement GetHashCode method, I am afraid this is the implementation you would need to have.

    public override int GetHashCode() 
    { 
        return 1; 
    } 
    

    Yes, this is inefficient but this is at least correct.

    The problem is that GetHashCode is being used by Dictionary and HashSet collections to place each item in a bucket. If hashcode is calculated based on some mutable fields and the fields are really changed after the object is placed into the HashSet or Dictionary, the object can no longer be found from the HashSet or Dictionary.

    Note that with all the objects returning the same HashCode 1, this basically means all the objects are being put in the same bucket in the HashSet or Dictionary. So, there is always only one single bucket in the HashSet or Dictionary. When trying to lookup the object, it will do a equality check on each of the objects inside the only bucket. This is like doing a search in a linked list.

    Somebody may argue that implementing the hashcode based on mutable fields can be fine if we can make sure fields are never changed after the objects added to HashCode or Dictionary collection. My personal view is that this is error-prone. Somebody taking over your code two years later might not be aware of this and breaks the code accidentally.

提交回复
热议问题