Why is it possible to use anonymous classes when LINQ multi-conditional joins in C #?

Source: Internet
Author: User
Tags joins

Today's whim, and suddenly want to understand why LINQ in the multi-conditional join, the use of the new anonymous class can be successful, this is why? Why is LINQ using equals instead of the common "= ="?

Subsequently, the following tests were performed, and it was found that equals did not handle the anonymous class object and the named class object in a consistent way

To learn more before discovering that

In C #, the anonymous class is rewritten so that the conditions in the first LINQ can be judged successfully. However, according to the test, to make two anonymous objects equal, attribute properties, property names, and property values must be identical.

Learn more about why equals behaves differently to named class objects than anonymous class objects, discovering that equals is only a virtual method of object, that the custom class in the test does not override equals, and then calls the objects method and returns false. At the same time, can also refer to this friend's article to learn more.

As below, the difference between "= =" and equals is found from So,

So, the content of the two objects that is usually compared to equals, "= =" Compares whether the two are the same thing. In a custom class, if you want to use object comparisons, you can override equals to control the judgment logic as needed.

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.