C# operator == check for null -
C# operator == check for null -
this question has reply here:
how check nulls in '==' operator overload without infinite recursion? 7 answersi have created class have functionality == operator, test if values null, when test start never ending loop. how can next without creating never ending loop?
public struct myclass { private string value; public static bool operator ==(myclass left, myclass right) { if (left == null && right == null) homecoming true; if (left == null || right == null) homecoming false; homecoming left.equals(right); } }
found anwser
public struct myclass { private string value; public static bool operator ==(myclass left, object right) { // test if both null or same instance, homecoming true if (referenceequals(left, right)) homecoming true; // if 1 of them null homecoming false if (((object)left == null) || ((object)right == null)) homecoming false; // test value homecoming left.equals(right); } } c# operator-overloading
Comments
Post a Comment