How to override the gethashcode Method

Source: Internet
Author: User

Analyze problems

The gethashcode method returns a hash value based on the current object and can be used in data structure algorithms or hash algorithms. The basic requirement of the gethashcode algorithm is that the same value must be returned when the same type object calls gethashcode. Furthermore, equal objects must return the same value, in this way, the hash value can be used in the container algorithm.

The gethashcode method has a default implementation in the object type. The basic idea is to use an internal object index member to generate hash values. The object index will never be repeated or changed, but the same object only has one index, which ensures the most basic requirement of gethashcode: the same value is returned for the same object. In fact, the default Implementation of gethashcode never meets this extended demand for all value types.

Description

The equality of objects means that the equals method is called to return true. This is also a warning generated during the previous code compilation. The equals method is overwritten, And the compiler cannot determine whether the objects that are originally considered equal and will generate the same hashcode will return true in the new comparison method.

When the custom type needs to be used as the key value of the hash table, the gethashcode method needs to be rewritten, especially when the new type is the value type or the new type overrides the equals method. As mentioned in the previous sections of this book, rewriting gethashcode has three important principles:

1. Call gethashcode to return an equal value for an equal object.

2. Call the gethashcode method to return the same value for the same object at any time.

3. Hash distribution of output values of all objects as much as possible.

Implementing the gethashcode Algorithm in an object does not guarantee the first principle of the preceding three items. This is because the original base class cannot obtain sufficient information of specific types, the equals method cannot be overwritten. The following code is an example of rewriting gethashcode.

Using system; namespace test {class overridegethashcode {public int _ Myint; Public readonly string _ mystring; Public override bool equals (Object OBJ) {If (OBJ = NULL) {return false;} If (object. referenceequals (this, OBJ) {return true;} If (this. getType ()! = Obj. getType () {return false;} overridegethashcode current = OBJ as overridegethashcode; If (_ Myint = current. _ Myint & _ mystring = current. _ mystring) {return true;} return false;} public overridegethashcode (int I, string s) {_ Myint = I; _ mystring = s ;} // override the gethashcode method public override int gethashcode () {// rely on the hashcode return _ mystring of read-only members. gethashcode ();} static void main () {// verify that the same object returns the same hashcode int I = 10; string S = "I Am a string "; overridegethashcode O1 = new overridegethashcode (I, S); overridegethashcode O2 = new overridegethashcode (I, S); console. writeline ("whether the object is equal: {0}", (o1.equals (O2 )). tostring (); console. writeline ("equals object hashcode equals: {0}", (o1.gethashcode () = o2.gethashcode ()). tostring (); // verify that the same object returns an equivalent hashcode int code = o1.gethashcode (); O1. _ Myint = 12; console. writeline ("whether the same object hashcode is equal: {0}", (o1.gethashcode () = Code ). tostring (); console. read ();}}}

In the above Code, overridegethashcode overwrites the equals method, implements content comparison, and overwrites the gethashcode method. In the main method, the Code verifies the algorithm by rewriting the first two principles of gethashcode. However, the algorithm of the above Code is not ideal for the hash distribution of hashcode, because objects with different _ Myint values but the same _ mystring value have the same hashcode. In the above Code, you need to pay special attention to the following points:

1. The new gethashcode algorithm depends on the gethashcode algorithm of read-only members to ensure the second principle.

2. The algorithm cannot ensure that different objects return different hashcodes. This does not completely violate the gethashcode principle, but makes the type have poor performance in applications in the hash.

3. Not all types have read-only members, so it is usually very difficult to rewrite gethashcode. In any case, ensuring the correctness of the algorithm is more important than ensuring its efficiency.

Answer

The gethashcode algorithm in the object ensures that the same hashcode is returned for the same object, while different objects return different hashcode. However, when objects with the same value type and content are of the same object type, the default gethashcode algorithm is incorrect.

Override gethashcode must ensure that the same hashcode value is returned for the same object at any time, and the same value must be returned for the same object. In addition, the hashcode should be randomly hashed.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.