Comparison of similarities and differences between three methods (rpm) defined in System.Object

Original: https://www.cnblogs.com/wangjinpeng-study/p/3913892.html

analyse problem

  In the previous chapters of this book, the author has been introduced in all the methods of System.Object. There are three methods of which are designed to perform a comparison target, are:

  (1)、static bool ReferenceEquals(object A,object B)。

  (2)、static bool Equals(object A, object B)。

  (3)、virtual bool Equals(object obj)。

  1, static methods: ReferenceEquals implements comparison reference type, it can be seen from the parameter type, which can be used to compare not only two types of reference objects can be used to compare two values ​​type objects. Of course, depending on the value types and reference types of features, ReferenceEquals method can be meaningful only when applied on a reference type, the type of reference to compare two values ​​will always return false, regardless of their values ​​are equal. When provided in the following code is executed:

  

int i=0;
Console.WriteLine(object.ReferenceEquals(i,i));

  The result will still be false, the reader can try to principle from boxing and unboxing to think about why.

  2, static methods: Equals is another static comparison method, which implements the functions differ depending on the type. In fact, the function of the Equals method relies on the implementation instance Equals method, fact speaking, the content of the static Equals method in two steps: First, check whether the two identical objects (==), then call one of the arguments object instance Equals method to determine whether two objects are identical.

note

  For value and reference types, checking whether the two identical objects (==) concept is different. For reference types, such a check is whether the reference refers to equal, and for value types, inspections are carried out for content.

  Using a transmitter, it can be clearly seen static method Equals code:

public static bool Equals(object objA,object objB)
{
  return ((objA==objB)||(((objA!=null)&&(objB!=null))&&(objA.Equals(objB)));
}

  As I previously analyzed, using the first method Equals == operator to check whether the parameter object are equal, then the inspection null reference and calls the instance method Equals compared. Such functions implemented entirely depends on the implementation of the Equals instance method, so readers will not need to hide static Equals method, but should focus on the immediate instance method Equals.

  3, instance methods: Equals actually did most of the work of comparison, it is a virtual instance method, you need to construct a custom comparison, you need to write from the Equals method. The following is mscorlib.dll intermediate code, it shows the default Equals method implemented in Object:

  

  Looks very simple, call InternalEquals method to check the memory address of the two objects are equal, that is, the default Equals implementation is a reference to compare equal. The author has been introduced in the previous chapter, the comparison reference in some cases is not practical, and the content is more likely to be more in line with the needs of the business logic, and this time, we need programmers to write their own type from the instance Equals method. In fact, all value types have override base class ValueType example Equals method. The following code is an example of a rewritable.

  

Copy the code
the System the using; 

namespace TestOverrideEquals 
{ 
    class OverrideEquals 
    { 
        static void the Main () 
        { 
            the MyObject the MyObject new new O1 = (10, "I am a string"); 
            the MyObject the MyObject new new O2 = (10, "I am a string"); 
            // Print comparison reference 
            Console.WriteLine ( "comparative reference: {0}", Object.ReferenceEquals (O1, O2) .ToString ()); 
            // print comparison result 
            Console.WriteLine ( "customized content comparison: {0} ", o1.Equals (O2) .ToString ()); 
            Console.Read (); 
        } 
    } 

    class the MyObject 
    { 
        Private int _MyInt; 
        Private String _MyString;

        public MyObject() { }
        the MyObject public (I int, String S) 
        { 
            _MyInt = I; 
            _MyString = S; 
        } 

        public BOOL the override Equals (Object obj) 
        { 
            // check null reference 
            IF (obj == null) 
            { 
                return to false; 
            } 
            // do the comparison reference how the two references are equal, it is bound content equal 
            iF (Object.ReferenceEquals (the this, obj)) 
            { 
                return to true; 
            } 
            // check if both types are equal 
            // consider the impact of inheritance, run-time type may not be the MyObject 
            // so use reflection to get the exact type of sacrifice performance to ensure the correctness 
            if (this.GetType ()! = obj.GetType ())
            { 
                Return to false; 
            } 
            // Contextual achieve 
            the MyObject AS the MyObject obj = right; 
            
            IF (_MyInt right._MyInt && == 
                // String type is a reference type, although, but to achieve a comparable content == 
                _MyString == right._MyString) 
            { 
                to true return; 
            } 
            return to false; 
            
        } 
    } 
}
Copy the code

  In the above code, the MyObject rewrite System.Object instance Equals method, implemented on a comparison of the contents. Implementations may get this result:

  

note

  Compile the above code will produce a warning, because the MyObject from writing the Equals method, but does not override the GetHashCode method to do so will have potential pitfalls, as will be covered in later chapters.

answer

  ReferenceEquals static methods to achieve a reference comparison. Static Equals method calls the instance Equals method to achieve a relatively efficient function. Examples Equals method is a virtual method, the default implementation is a reference type, type instance Equals method can be overridden if necessary. ValueType value type base class overrides the method Equals realized content comparison.

Guess you like

Origin www.cnblogs.com/zengyu199507/p/12556211.html