ReferenceEquals,==,Equals比较

 
 
ReferenceEquals,==,Equals比较

 ReferenceEquals, ==, Equals 
   individual Equals, ==, ReferenceEquals can be used to judge the object is not equal to two.    a) ReferenceEquals 

ReferenceEquals static Object methods for comparing two references type of object is a reference to the same object. For value types it always returns false . (Because after the Box object is always different, hehe)


   b) == is a binary operator can override, may be used to compare whether two objects are equal.    For built value types, the algebraic value is determined == two objects are equal. It will be necessary type conversions automatically as needed, and returns true or false depending on the value of the two objects are equal. For example:    Int = 100 A;    Double B = 100;    the If (A == B)    Console.WriteLine ( "the Supports equal Compare Different types BETWEEN!");    Above this program will output:    equal the Supports Compare Different types BETWEEN! And for user-defined value type, if not override operator == == will not be able to use . For example:    Struct Userstruct1;    Userstruct1 A;    Userstruct1 b;    the If (A == b)    Console.WriteLine ( "CAN == the this the REACH FAR?")    The code above is not able to compile the. Override Enable == by acting on the user-defined value types.    For reference types, the default behavior with the same == ReferenceEquals behavior, only two objects point to the same time of Reference before returning true. But  
 
 
 
 
 
 
 
    
 
 
 
 
 
 
.NET Many of the Framework class == is overloaded, for example, the same == Equals String class behavior, determines the content of the two strings are equal. Therefore, applications for system reference defined type is not recommended to use the == operator, so as not to appear and the program is expected to run a different result.


   c) Equals Object as a built-in method, Equals support for any two comparison objects CTS.    It is a static method Equals and overloaded version, the following program fragment explains the usage of these two methods,    int. 5 = A;    int = B. 5;    the If (the Object.Equals (A, B))    // you Also use IF CAN (between a.Equals (B))    {    Console.WriteLine ( "iS a equal to B");    }    in fact, the two versions are identical results, if the user Equals is overloaded, calls are Equals the user overload. Benefits Equals static methods that can be used regardless of whether the comparison object is null. Equals Method value and reference types for different definitions, value types, the type of the same , and the same value (for each member of the struct must be the same), the Equals returns true, otherwise returns false. For reference types, the same as the default behavior ReferenceEquals behavior, only two objects point to the same time of Reference before returning to true . Equals Overload can be required, for example for determining Equals String class contents of the two strings are equal.    A new new = the StringBuilder the StringBuilder ();    a.Append ( "The Test A"); 
 
 
 
 
 
 
 
 
 
    
 
 
   S1 = a.ToString String ();    String S2 = "The Test A";    IF (== S1 S2) // is true because S2, S1 is equal to the content. It equals the same as the String class == behavior is determined whether the contents of the two strings are equal    Console.WriteLine ( "== Returns to true");    IF (the Object.Equals (S2, S1)) // true, String Class Equals overloaded, for determining the contents of the two strings are equal    {    Console.WriteLine ( "Returns the equals to true");    }    iF (Object.ReferenceEquals (s2, s1)) // false, because s2, s1 and not refer to the same object    {    Console.WriteLine ( "ReferenceEquals Returns to true");    }    this example will output:    == Returns to true    the equals Returns to true    NOTE: for String class, direct statement s1 = "the test a", then the output will It contains    "ReferenceEquals returns true",    because the default, string declared for the same string on the heap but one Copy, so s1 and s2 will point to the same Reference 
 

 

 
 
 

 
 
 
 
 
 
 
 

 

 

如果重写了equals方法,gethashcode方法也要进行重写。

转载于:https://www.cnblogs.com/Jenny90/archive/2013/04/13/3018456.html

Guess you like

Origin blog.csdn.net/weixin_34235135/article/details/93566820