Study C # advanced programming together 2 -- compare the equality of objects,

Source: Internet
Author: User

Study C # advanced programming together 2 -- compare the equality of objects,

In the future, we will strive to update every two days. There are too many temptations on weekdays, so I have to take a long time off, and the progress is a little slow.

As mentioned above, the type security leaves a small tail-comparing the equality of objects.

C # There are four equal comparison methods: Except for the "=" operator, System. Object defines method 3: ReferenceEqual () and two Equals ();

1. The first is the "=" OPERATOR:

For the value type, "=" compares the two values for equality; for the reference type, "=" compares the two objects for the same reference address. Here is a special case, that is, the string type. "=" is to compare the string value rather than the reference address, because the system overwrites the "=" operator. The reload of operators will be discussed later.

2. ReferenceEqual () method

ReferenceEqual () is a static method used to compare whether two objects reference the same instance of the class, that is, the address referenced from the same memory. As a static method, it cannot be overwritten. True is returned if two references are referenced from the same object instance. Otherwise, false is returned.

  

SomeClass s1 = new SomeClass ();
SomeClass s2 = new SomeClass ();
SomeClass s3 = s1;
Bool b1 = ReferenceEquals (null, null );
Bool b2 = ReferenceEquals (s1, s2 );
Console. WriteLine (b1); // true
Console. WriteLine (b2); // false. Different instances are created with different reference addresses.
Console. WriteLine (ReferenceEquals (s1, s3); // true, s1, 32 reference addresses are the same
Console. WriteLine (ReferenceEquals (1, 1); // flse, because it is not a comparison value, but a reference address

Note:

Because ReferenceEquals () is a reference type, when comparing value types, it will take the boxing (mentioned in the previous section) operation to convert the value type to the reference type, therefore, false is returned.

Bool B = ReferenceEquals (); returns false.

 

3. Virtual Equal () method: Equals (Object)

Determines whether the specified Object is equal to the current Object.

Because it is a virtual method, it can be overwritten. Similar to the "=" operator, the comparison value type and reference type are supported, where string is compared as a value type

 

4. Static Equals () method: Equals (Object, Object)

Determines whether the specified object instance is considered equal.

 

Equals method, the book is just a brief introduction to the next, detailed answers can refer to the MSDN documentation: http://msdn.microsoft.com/zh-cn/library/System.Object.Equals (v = vs.100 ). aspx. The two methods of Equals () are described in detail.

 

Next, reload operators.

  

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.