Measure the test taker's knowledge about the differences between Equals and =.

Source: Internet
Author: User
Tags mscorlib

First, I recommend that you read the difference between Equals and = before proceeding to this article (although some explanations in this article are still a bit difficult ), I wrote this article only because of the issues that have arisen in my article @ Yimu sunshine. Of course, you can also read this article directly, because I have not forcibly linked this article with it.

Let's take a look at the following code:

1             int i1 = 8;2             int i2 = 8;3             bool bo1 = i1 == i2;                    // true4             bool bo2 = (object)i1 == (object)i2;    // false5             bool bo3 = i1.Equals(i2);               // true6             bool bo4 = i1.Equals((object)i2);       // true7             bool bo5 = ((object)i1).Equals(i2);     // true

  1. bo1 = true.There is no doubt that the value type comparison is to compare its value.

 

2. bo2 = false.This is to pack the two int type values. When "=" is compared, it compares the referenced address, so it is false.

  3. bo3 = true.This is to call the Equals (int) method of Int32 corresponding to int:

1 public bool Equals(int obj)2 {3     return (this == obj);4 }

Obviously, this is i1, obj is i2, and i1 = i2 is returned, true.

  4. bo4 = true.We often think that Equals (object) is inherited from the Object class:

1 public virtual bool Equals(object obj)2 {3     return RuntimeHelpers.Equals(this, obj);4 }

However, it is often ignored that it is a virtual method, such as Int32, UInt32, Double, String, and other common types, all rewrite this method, and the Int32 type corresponding to int is rewritten:

1 public override bool Equals(object obj)2 {3     return ((obj is int) && (this == ((int) obj)));4 } 

Therefore, when i1.Equals (i2) is encountered, first call (obj is int) to determine whether i2 is of the int type. If so, a forced conversion occurs. In fact, the returned result is i1 = (int) (object) i2), visible, which must be true.

The rewriting of other types is similar to this. The system first checks whether the obj parameter is of the current type. If yes, it forces conversion and then compares it. If not, it is processed based on the actual situation. Here, we can see two main situations: the first one is the value type. Try to convert the type of the parameter. If the parameter is of the same type as its own, call the = sign directly to compare it (in special cases). If the parameter is of a different type, return false; second, it is a reference type (note that string is a reference type). It will try to convert the parameter to its own type. If it fails, it will directly return false; otherwise, it will compare whether the referenced address is the same,. String is a special case in the reference type. Not only can strings with the same referenced address be true, But strings with the same content are also true. You can refer to the following Equals (object) Rewriting Method for the string type:

 1 public override bool Equals(object obj) 2 { 3     if (this == null) 4     { 5         throw new NullReferenceException(); 6     } 7     string strB = obj as string; 8     if (strB == null) 9     {10         return false;11     }12     if (object.ReferenceEquals(this, obj))13     {14         return true;15     }16     if (this.Length != strB.Length)17     {18         return false;19     }20     return EqualsHelper(this, strB);21 }

The final EqualsHelper (this, strB) function internally compares the content of the two strings by character.

  5. bo5 = ture.This is not the same as that of bo4 and bo3. At this time, the compiler calls not the Int32 Equals (int) or Equals (object) method, but the Object type Equals (object) method. It is learned through the IL code that i1 is manually packed through (object) i1, while i2 is co-variant and automatically packed:

1 IL_0032: ldloc.0 // read i02 IL_0033: box [mscorlib] System. int32 // boxed 3 IL_0038: ldloc.1 // read i14 IL_0039: box [mscorlib] System. int32 // Pack 5 IL_003e: callvirt instance bool [mscorlib] System. object: Equals (object) // call Object. equals (object) Method 6 IL_0043: stloc. s bo5 // Save the result to bo5

At this time, why is it equal? Note that there is only one row in the code of the Object mentioned above: RuntimeHelpers. equals (this, obj); I used Reflector and did not find the specific implementation. The visual test should be used to help the compiler achieve code generation in the runtime, the conversion may be implemented internally, and the final comparison will be completed by calling the Int32 Equals Method Based on the i1 type at runtime. To verify my ideas, I found the following RuntimeHelpers. Equals (object, object) on MSDN:

RuntimeHelpers class

Provides a set of static methods and attributes that support the compiler. This class cannot be inherited.

RuntimeHelpers. Equals method (Object, Object) Return Value Type: System. Boolean

If the o1 parameter and o2 parameter are the same instance, or both are null, or the o1.Equals (o2) returns true, the value is true; otherwise, the value is false.

Because (object) i1 and (object) i2 are not the same instance, neither of them is null, so they enter the logic operation phase of i1.Equals (i2), and finally return true. This is the end of this article. In this way, I can at least find out what actually happened, without having to memorize it, it will not be overwhelmed in this case.

Reprinted please indicate the original address: http://www.cnblogs.com/lekko/archive/2013/03/06/2946282.html

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.