C # iequatable == example, why use the (object) myInstace to check for null

So, in one example given in C #.

https://docs.microsoft.com/en-us/dotnet/api/system.iequatable-1.equals?view=netframework-4.7.1

They add an ==-operator to the class, but the part I don’t understand is why they use (object)target == nullto check for the zero sequence instead target == null.

public static bool operator ==(PotentialGrabTarget target1, PotentialGrabTarget target2)
{
    //~~~~~ My Question is for the next line of code ~~~~~
    if (((object)target1) == null || ((object)target2) == null) //<<<<<<                         
        return System.Object.Equals(target1, target2);

    return target1.Equals(target2);
}
+4
source share

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


All Articles