I changed the intent of the overridden operator `==`?

The following overloaded statement ==is part of the class Calenderin QL.net

    public static bool operator ==(Calendar c1, Calendar c2)
    {
        return (c1.empty() && c2.empty()) 
                || (!c1.empty() && !c2.empty() && c1.name() == c2.name());
    }

    public bool empty() { return (object)calendar == null; }    

When I try to access a property SouthAfricanCalender, I get System.NullReferenceException : Object reference not set to an instance of an object.that prompted me to delve into the source.

public SouthAfrica SouthAfricanCalender
{
    get
    {
        if (_calender == null)
        {
        _calender = new SouthAfrica();
        }
    return _calender;
    }
    set
    {
        if (_calender == null)
        { 
        _calender = value;
        }
    }
}

SouthAfrica _calender;

I overloaded the overload as follows based on the answer here

public static bool operator ==(Calendar c1, Calendar c2)
{
   if ( object.ReferenceEquals(c1,c2)) return true;
   if ((object)c1 == null || (object)c2 == null) return false;

   return (c1.empty() && c2.empty())
       || (!c1.empty() && !c2.empty() && c1.name() == c2.name());
}

My question is, have I changed the intention of the source code with my amendment?

Edit: any suggestions on how this can be cleared further?

+3
source share
2 answers

. , , ( , ReferenceEquals ). . () , , , , .

+1

, .

- .

+1

Source: https://habr.com/ru/post/1780433/


All Articles