C # Learning: Equality Comparisons for objects

Source: Internet
Author: User
Tags bool comparison

One, reference type equality comparison:

A total of four comparison methods:

1:referenceequals () method, static method, that tests whether two references specify the same object. And that null equals NULL.

1 Class1 x,y;
2 x = new Class1 ();
3 y = new Class1 ();
4 bool B1 = ReferenceEquals (null,null);//true
5 bool B2 = ReferenceEquals (null,x);//false
6 bool B3 = ReferenceEquals (x,y);//false

2: Virtual Equals () method, virtual, can rewrite, compare objects by value.

3: Static Equals () method, two parameters, compared to them (and virtual equals () difference).

4: comparison operator = =, which can be viewed as an intermediate option between strict value comparisons and strict reference comparisons.

Two, value type equality comparison:

The theoretical value comparison can be compared with drinking the same method, except that the value type requires a boxing process. However, in the boxing process, each value is individually boxed,

Therefore, it is meaningless to always return false with refernceequal () comparing the value type.

Summary: By understanding the type of drinking and value types, you can understand how the above works. Now the concept of equals () is still a little blurry, and hopefully it will be understood in the application.

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.