Is Object.GetHashCode() unique to a reference or a value?

后端 未结 6 990
南方客
南方客 2020-12-24 12:58

The MSDN documentation on Object.GetHashCode() describes 3 contradicting rules for how the method should work.

  1. If two objects of the same type represent the
6条回答
  •  轮回少年
    2020-12-24 13:39

    Rules 1 & 3 aren't really a contradiction.

    For a reference type the hash code is derived from a reference to the object - change an object's property and the reference is the same.

    For value types the hash code is derived from the value, change a property of a value type and you get a completely new instance of the value type.

提交回复
热议问题