C # overload operators = and! = Notes

Source: Internet
Author: User

[Csharp] public class MyClass {public int ID;/* public static bool operator = (MyClass c1, MyClass c2) {// The "=" call in the following code enters recursion until the crash if (c1 = null | c2 = null) {return c1 = c2 ;} return c1.ID = c2.ID;} public static bool operator! = (MyClass c1, MyClass c2) {// The "=" call in the following code enters recursion until the crash if (c1 = null | c2 = null) {return c1! = C2;} return c1.ID! = C2.ID;} * // correct public static bool operator = (MyClass c1, MyClass c2) {// convert the c1 object into an object and then judge it. // the object here. equals (c1, null) can also be written as // c1 as object = null if (object. equals (c1, null) | object. equals (c2, null) {return object. equals (c1, c2);} return c1.ID = c2.ID;} public static bool operator! = (MyClass c1, MyClass c2) {if (object. Equals (c1, null) | object. Equals (c2, null) {return! Object. Equals (c1, c2);} return c1.ID! = C2.ID ;}}

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.