About warning CS0659: "* * *" overriding Object.Equals (object o) but not overriding Object.GetHashCode ()

Source: Internet
Author: User
Tags shallow copy

Equality and identity of objects
  • The System.Object type provides the following methods,
  • 1 namespaceSystem2 {3     //4     //Summary:5     //supports all classes in the. NET Framework class hierarchy and provides low-level services for derived classes. This is the final base class for all classes in the. NET Framework, which is the root of the type hierarchy. 6 [ClassInterface (classinterfacetype.autodual)]7[ComVisible (true)]8      Public classObject9     {Ten         // One         //Summary: A         //Initializes a new instance of the System.Object class.  - [ReliabilityContract (Consistency.willnotcorruptstate, Cer.mayfail)] -          PublicObject (); the  -         // -         //Summary: -         //allows an object to attempt to free resources and perform other cleanup operations before garbage collection is reclaimed.  + [ReliabilityContract (Consistency.willnotcorruptstate, cer.success)] -~Object (); +  A         // at         //Summary: -         //determines whether the specified object instance is considered equal.  -         // -         //Parameters: -         //Obja: -         //the first object to compare.  in         // -         //OBJB: to         //the second object to compare.  +         // -         //return Result: the         //true if the object is considered to be equal; otherwise false. If both Obja and OBJB are null, the method returns True.  *[Targetedpatchingoptout ("performance critical to inline across NGen image boundaries")] $          Public Static BOOLEquals (Object Obja, Object OBJB);Panax Notoginseng         // -         //Summary: the         //determines whether the specified System.Object instance is the same instance.  +         // A         //Parameters: the         //Obja: +         //the first object to compare.  -         // $         //OBJB: $         //the second object to compare.  -         // -         //return Result: the         //true if Obja is the same instance as OBJB, or if both are null; otherwise, false.  - [ReliabilityContract (Consistency.willnotcorruptstate, cer.success)]Wuyi[Targetedpatchingoptout ("performance critical to inline across NGen image boundaries")] the          Public Static BOOLReferenceEquals (Object Obja, Object OBJB); -         // Wu         //Summary: -         //determines whether the specified object is equal to the current object.  About         // $         //Parameters: -         //obj: -         //the object to compare with the current object.  -         // A         //return Result: +         //true if the specified object is equal to the current object; otherwise false.  the[Targetedpatchingoptout ("performance critical to inline across NGen image boundaries")] -          Public Virtual BOOLEquals (Object obj); $         // the         //Summary: the         //as the default hash function.  the         // the         //return Result: -         //the hash code for the current object.  in[Targetedpatchingoptout ("performance critical to inline across NGen image boundaries")] the          Public Virtual intGetHashCode (); the         // About         //Summary: the         //gets the System.Type of the current instance.  the         // the         //return Result: +         //the exact run-time type of the current instance.  - [SecuritySafeCritical] the          PublicType GetType ();Bayi         // the         //Summary: the         //returns a String that represents the current object.  -         // -         //return Result: the         //A string that represents the current object.  the          Public Virtual stringToString (); the         // the         //Summary: -         //creates a shallow copy of the current System.Object.  the         // the         //return Result: the         //a shallow copy of the current System.Object. 94 [SecuritySafeCritical] the         protectedObject MemberwiseClone (); the     } the}
    Syste.object
  • It provides a virtual method named Equals that returns True if two objects are equal.
  • First implementation of the Equals method

    It seems to be a reasonable implementation, but the problem is that if an argument refers to a different object, equals cannot determine if the object contains the same value, it will be judged false. For the default implementation of the equals of object, it implements identity, not equality.

  • For the following questions, 1) If the obj argument is null,2) this and the obj arguments are references to the same object, 3) this and the obj arguments refer to different types of objects, implementing the Equals method of object.
  • 1   Public classObject2     {3          Public VirtualBoolean Equals (Object obj)4         {5             if(obj = =NULL)6                 return false;7             if( This. GetType ()! =obj. GetType ())8                 return false;9 Ten             return true; ; One         } A}
    Equals Method
  • When a type overrides the Equals method, the overridden method should call the base class's equals implementation (unless the base class is an object). Another type can override the Equals method of object, so you can't test the identity by calling this equals method. Object provides a static method ReferenceEquals, which is prototyped as follows:
  • 1   Public Boolean ReferenceEquals (Object Obja, Object OBJB) 2     {3         return (Obja = = objb); 4     }
    ReferenceEquals

    If you want to check the identity (see if two references point to the same object), be sure to call referenceequals instead of c # = = operator (unless you first convert two operands to object)

  • The ValueType Equals method uses reflection technology, because the CLR's reflection mechanism is slow, so when defining your own value types, you should override the Equals method to provide your own implementation to improve performance when comparing values with instances of a type. Of course, in your own sight, do not call base. Equals.
  • When defining your own type, if you decide to override Equals, you must determine that it conforms to the 4 characteristics of equality.
  • 1) equals must be reflexive.
  • 2) equals must be symmetric.
  • 3) equals must be transitive.
  • 4) equals must be consistent.
  • To override the Equals method, also:
  • the Equals method that lets the type implement the System.iequatable<t> interface. This generic interface allows you to define a type-safe equals method.
  • The overloaded = = and! = operator methods should usually be used in advance of these operator methods to internally invoke the type-safe equals method.

In addition, if later for the purpose of sorting to compare the type of instances, then the type of system.icomparable to implement the CompareTo method and System.icomparable<t> CompareTo method.

Object Hash?

System.Object provides a virtual method GetHashCode, which can get the Int32 hash of any object.

  • If you define a type that overrides the Equals method, you should also override the GetHashCode () method. If you define a type that overrides equals without overriding GetHashCode (), Microsoft's C # compiler reports a warning message, such as the following type, which displays a warning message:warning CS0659: "Program" Override Object.Equals (object o) but do not rewrite Object.hethashcode ().
  • overriding equals

    The reason for defining GetHashCode at the same time is because of the System.Collections.Hashtable type, The implementation of the System.Collections.Generic.Dictionary type and some other collections requires that two objects must have the same hash in order to be equal, so if you override Equals, you should also override GetHashCode, Ensure that the equality algorithm and the object hash algorithm are consistent.

  • 1 Internal Sealed class Point2     {3         PrivateInt32 m_x, m_y;4          Public Override intGetHashCode ()5         {6             returnm_x ^m_y;7         }8}
    GetHashCode

About warning CS0659: "* * *" overriding Object.Equals (object o) but not overriding Object.GetHashCode ()

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.