Search code examples
c#gethashcode

Why is used Connection.GetHashCode in C#?


When I examined a project(my company codes). I looked this:

public override int GetHashCode()
{
    unchecked
    {
        int result = 17;
        result = result * 23 + ((connection != null) ? this.connection.GetHashCode() : 0);
        return result;
    }
}

actually, I have seen GetHashCode() first time. I ran a little about it. But I cant understand why they used it in this code line and for connection?

Is there a special reason? What is the logic to use getHashCode for connection?

Thanks.


Solution

  • That GetHashCode can be used to quickly identify whether two instances appear to be "the same" (different hashcode = different instance; same hashcode could mean same instance).

    Apparently that "connection" field is important in deciding the identity of your object.