=, Object. Equals (), object. referenceequals ()

Source: Internet
Author: User

 Reference: http://www.cnblogs.com/pursue/articles/1614285.html

 

Sometimes two values must be compared for equality.In some cases, you test "equality of values" (also known as "equivalence"), meaning that the two variables contain equal values.In other cases, you must determine whether the two variables reference the same base object in the memory.This type of equality is called "reference equality" (or "identifier ").

 

1. Object. referenceequals (Object obja, object objb)

Purpose: compare whether two reference types of objects are referenced to the same object, that is, reference equality.

(1)First, check whether obja and objb are null. If only one of them is null, false is returned. If both are null, true is returned.

(2) If obja and objb are value types, bind them to different object instances first. Therefore, for the two value types, false is returned regardless of whether their values are equal;

(3) If both obja and objb are strings, the strings are referenced and irrelevant to the values. However, the C # string pool mechanism makes the string comparison somewhat special. For more information, see the supplementary section below.

Supplement:

Assign values to multiple references to the same string object. The CLR solution is more clever. By default, CLR uses the string pool mechanism. When CLR is started, a container is created internally and exists as a key-value pair. The key value is the content of a string object, the value is a string reference on the hosting stack. When a New String object is created, the CLR checks whether the string object already exists in these values. If yes, the corresponding value is returned, that is, the reference in the hosting heap. If it does not exist, it will open up space in the container to store this string and return the reference in it.
Here we still use a Section Code To describe:
Public void stringpooltest ()
{
String str1 = "789 ";
String str2 = "789 ";
Console. writeline (object. referenceequals (str1, str2 ));
String str3 = "7" + "8" + "9 ";
Console. writeline (object. referenceequals (str1, str3 ));
Char [] chars = new char [] {'7', '8', '9 '};
String str4 = new string (chars );
Console. writeline (object. referenceequals (str1, str4 ));
}
Output result:

The previous two outputs true indicate that both str1 and str2 point to the same heap reference. The last output is false because it uses the New Keyword for memory allocation, and the string pool mechanism does not work.
Note here that string str3 = "7" + "8" + "9"; normally, this statement only allocates memory once, which is also the CLR optimization, no memory is allocated here, because str1 has been allocated.

2.Object. Equals (Object obja, object objb)

Purpose:ComparisonAny two CTS objects.

(1) The equals method has different definitions for the value type and reference type,For the value type, the type is the same, and the value is the same (for each struct member must be the same) (that is, bitwise equality, binary representation), equals () returns true, otherwise, false is returned;For non-stringReference type. The default behavior is the same as that of referenceequals (). True is returned only when two objects point to the same reference. However, for the special reference type of string,EqualsThe value of the string is compared, rather than referenced. For details, see the following..

Output:

(2) equals has two versions: static method and Rewritable instance method. If the equals () method of the type is overwritten, the rewriting method is called. Otherwise, the equals () of the base class object is called () instance method.

(3) It is worth noting that ifObja and objb are values (test code is as follows:Figure 1As shown in.EqualsAndThe referenceequals method is the same firstObja and objb are packed (the Il code is as follows:Figure 2As shown in)But the returned results are different. The returned results are as follows:Figure 3. (The reason is that when the equals method is used for value type comparison, it compares the bit values of the value type, that is, bitwise equality.)

Figure 1

Figure 2

Figure 3

 

 

3. =

= Is a binary operator that can be overloaded. It can be used to compare whether two objects are equal.
(1) For the built-in value type, = determines whether the generated values of the two objects are equal. It automatically performs necessary type conversion as needed, and returns true or false based on whether the values of the two objects are equal. For example:

Int A = 100;
Double B = 100;

If (A = B)
Console. writeline ("Equal supports compare between different types !");

The above sectionProgramThe output is as follows:
Equal supports compare between different types!

Note:Because the floating point on the binary computerAlgorithmSo the equality of the floating point values (double and float) may lead to problems.

 

(2) for user-defined value types, if the = operator is not overloaded, = is not usable. Example:
struct userstruct1;
userstruct1 A;
userstruct1 B;

If (A = B)
Console. writeline ("Can = reach this far ?")

The above Code cannot be compiled. You can use the overload to enable = to act on user-defined value types.

 

(3) For non-string reference types, = the default behavior andEquals,Referenceequals has the same behaviorReturns true only when two objects point to the same reference.However, many classes in. NET Framework overload =. For example, if the = of the string class is the same as the behavior, determine whether the content of the two strings is equal. Therefore, we recommend that you do not use the = operator for the reference type defined by the system in the application to avoid different running results in the program.

(4) For the string type,= Default behavior andEquals has the same behavior and compares the string value.

4.When comparing GetType () and typeof () of an object, you can use =, object. Equals (), and object. referenceequals.

  

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.