C # overloaded operator (ovveride operator) pits, about null

Source: Internet
Author: User

Scenario Description:

Needs to be identical to the corresponding objects of the two versions (consistent with each property value), inconsistent export report color identification, in order to remind the subsequent use of report personnel.

Implementation ideas:

The object overloads the ToString method, which implements a comparison to the base class (for general) overloading = = with the! = operator, all of which inherit caster against the base class.

Code:

classA:C { PublicAstringAstringb) {AA=A; BB=b; }         Public stringAA {Get;Set; }  Public stringBB {Get;Set; }  Public Override stringToString () {returnaa+BB; }    }    classB:C { PublicB () {} PublicBstringAstringb) {AA=A; BB=b; }         Public stringAA {Get;Set; }  Public stringBB {Get;Set; }  Public Override stringToString () {returnAA +BB; }    }    classC { Public Static BOOL operator==(c A, C b) {//here is a loophole, the object is compared, if it is and NULL, the following ... Oh, yes.            string_a =a.tostring (); string_b =b.tostring (); return_a.length = = _b.length && _a.gethashcode () = = _b.gethashcode () && _a = =_b; }         Public Static BOOL operator!=(c A, C b) {return true; }    }    classTest { Public voidCompare () {Console.WriteLine (NewA"a",NULL) ==NewA"a","b"));//NormalConsole.WriteLine (NewB"a","b") ==NewB"a","b"));//NormalConsole.WriteLine (NewB"a","b") ==NULL);//Well , this is a hit bug.        }    }

How to solve the loophole, the second edition of Class C:

classC { Public Static BOOL operator==(c A, C b) {//There's going to be a dead loop system.stackoverflowexception anomaly .            if(A = =NULL|| b = =NULL)            {                if(A = =b)return true; return false; }            string_a =a.tostring (); string_b =b.tostring (); return_a.length = = _b.length && _a.gethashcode () = = _b.gethashcode () && _a = =_b; }         Public Static BOOL operator!=(c A, C b) {return true; }    }

Why is it? After throwing an exception, understand, when judging a==null, do not also need to perform the overloaded after = =? Over and over again, the cycle of death.

Think about it, is there any solution? If there is a problem with the overload you are writing now, can you use an overloaded overload that does not matter? For example, all classes of the base class object, and Object==null is not reported exception, then consider the third version, as follows:

classC { Public Static BOOL operator==(c A, C b) {//borrowing object==null to judge, type conversion, time-consuming            if((A as Object) ==NULL)            {                return(b as Object) ==NULL; }            if((b as Object) ==NULL)                return false; string_a =a.tostring (); string_b =b.tostring (); return_a.length = = _b.length && _a.gethashcode () = = _b.gethashcode () && _a = =_b; }         Public Static BOOL operator!=(c A, C b) {return true; }    }

I do not pass the bottom of the things, but, the type conversion is really time-consuming, my side of the report is tens of thousands of data comparison, of course, I did not test, so think of a weight can be judged by the type of thinking, as follows:

classC { Public Static BOOL operator==(c A, C b) {//borrowing is judging type            if((A isC) && (b isC)) {string_a =a.tostring (); string_b =b.tostring (); return_a.length = = _b.length && _a.gethashcode () = = _b.gethashcode () && _a = =_b; }            return! (A isC) &&! (b isC); }         Public Static BOOL operator!=(c A, C b) {return true; }    }

Above is my train of thought, if which big God understands why object==null is no problem, also please inform, again thanked, welcome everybody message.

C # overloaded operator (ovveride operator) pits, about null

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.