Differences and connections between Equals, RefrenceEquals, and = in C #

Source: Internet
Author: User

In C #, determine whether two objects are equal: Equals, RefrenceEquals, and =, where = is the operator, and the other two are the methods, and Equals has two versions, one is static, the other is virtual, the virtual can be overwritten by the entity class, and the static is also called virtual in the method body, as shown below:
Copy codeThe Code is as follows:
Public static bool Equals (object objA, object objB)
{
Return (objA = objB) | (objA! = Null) & (objB! = Null) & objA. Equals (objB )));
}
Public virtual bool Equals (object obj)
{
Return InternalEquals (this, obj );
}
[MethodImpl (MethodImplOptions. InternalCall)]
Internal static extern bool InternalEquals (object objA, object objB );
[ReliabilityContract (Consistency. WillNotCorruptState, Cer. Success)]
Public static bool ReferenceEquals (object objA, object objB)
{
Return (objA = objB );
}

The above code is decompiled by reflector. Their differences and relationships are summarized as follows::
1. refrenceEquals, as its name implies, determines whether the references of two objects are equal under any circumstances. For value types, because a packing operation must be performed before each judgment, that is, a temporary object is generated each time, therefore, false is always returned. The String type is special. As long as the same character is always the same reference, the different characters are different references, even if the value is assigned by passing, such as: string str1 = "a"; string str2 = str1; str2 = "B"; str1 and str2 are still different references.

2. = There is no essential difference with Equals. They are the same in most cases. For the basic value type, it determines whether the value is equal. For the reference type, it determines whether the reference is the same. It is worth noting that the custom value type struct does not support the operator =. If you use it forcibly, a compilation error will occur. In addition, given that Equals is a virtual method, it can be overwritten by a specific class, so you need to analyze the specific problem.

3. as mentioned above, static Equals are essentially called virtual Equals. The difference between them is that when calling, virtual ones should consider whether the object is empty, otherwise an exception will be thrown, static data does not need to be considered.
The following is the test code:
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
// AAA a1 = new AAA {Name = "a1", Age = 22 };
// AAA a2 = new AAA {Name = "a1", Age = 22 };
// Int a1 = 123;
// Int a2 = 123;
String a1 = "abc ";
String a2 = "abc ";
Console. WriteLine (string. Format ("==:{ 0}", a1 = a2 ));
Console. WriteLine (string. Format ("Equals: {0}", a1.Equals (a2 )));
Console. WriteLine (string. Format ("Static Equals: {0}", Object. Equals (a1, a2 )));
Console. WriteLine (string. Format ("ReferenceEquals: {0}", ReferenceEquals (a1, a2 )));
Console. Read ();
}
}
// Class or Struct
Struct AAA
{
Public string Name {get; set ;}
Public int Age {get; set ;}
}

Related Article

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.