To override Equals, you must also override GetHashCode.

Source: Internet
Author: User

This is because if you rewrite the Equals method without overwriting the GetHashCode method, a potential Bug may occur when you use the Dictionary class. Copy the code static Dictionary <Person, string> personValues = new Dictionary <Person, string> (); protected void Page_Load (object sender, EventArgs e) {AddPerson (); person mike = new Person ("Mike"); Response. write (personValues. containsKey (mike); // False} void AddPerson () {Person mike = new Person ("Mike"); personValues. add (mike, "mike"); Response. write (personValues. containsKey (mike); // True} output result of this code segment: Tru E False: copy the Code. This Code indicates that when AddPerson () is executed, the idCode = Mike's Person object is stored in the Dictionary, and then in the Page_Load method, similarly, a new Person object with idCode = Mike uses the ContainsKey method to search for whether the object Key exists. The result is that the object does not exist. You can ask, in the previous suggestion, we have rewritten the Equals method of the Person class. As long as the idCode is equal, we can think that they are equal, why can't I find Mike here? A: This is because the CLR has optimized the query for the Dictionary type. In fact, the query for the Value is based on the HashCode of the Key Value. CLR first calls the GetHashCode method of the Person type, and finds that this item is not overwritten at all. Therefore, it searches for the GetHashCode method of the Object, and the Object provides the default GetHashCode implementation for all CLR types, every new object, CLR will generate a fixed integer value for this object, which will not be changed during the object lifecycle. The default GetHashCode Implementation of the object is the HashCode of this integer value. Therefore, although the Mike value is equal, the HashCode is not equal. To fix this problem, you must override the GetHashCode method public override int GetHashCode () {return this. IDCode. getHashCode ();} further improved: the GetHashCode method has a problem. It returns an integer type, and the Capacity length of the integer type is far from meeting the length of the string. That is to say, if the values are different, the HashCode may be the same. To reduce the generation of the same HashCode, make an improved version: public override int GetHashCode () {return (System. reflection. methodBase. getCurrentMethod (). declaringType. fullName + "#" + this. IDCode ). getHashCode ();} summary: This suggestion should at least let me know about HashCode. Previously, ToSt was rewritten. The GetHashCode is often seen during the ring method.

Related Article

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.