Does the order of operands matter for an overloaded == operator in C#

I am using a 3rd party tool (Unity3d), where one of the fundamental base classes overloads the == operator (UnityEngine.Object).

The overloaded operator's signature is:

public static bool operator == (UnityEngine.Object x, UnityEngine.Object y)

Does the order in which the comparison is made have any effect on whether this overloaded operator is used ?

To illustrate, should both of these use the overloaded operator and return the same result ?

// initialize with some value
UnityEngine.Object a = .... 

Debug.Log(a == null);
Debug.Log(null == a);

The reason i'm asking is because i'd like to (sometimes) avoid this overloaded behaviour (use the default == operator), and was wondering whether flipping the order would assist in that ?

(There could be another option - casting the operands to System.object, but I am not 100% sure that works).

Jon Skeet
people
quotationmark

Well, it's possible that the two calls wouldn't be the same, if the operator had been overloaded badly. Either there could be one overload, and it could be written in a way which compares the operands asymmetrically, or the two statements could call different overloads.

But assuming it's been overloaded properly, that should be absolutely fine. That's if you want to call the overloaded operator, of course. In situations where you don't, I'd make that clear using ReferenceEquals.

I would personally recommend the if (a == null) approach, as I find it easier to read (and I believe many others do too). The "yoda" style of if (null == a) is sometimes used by C programmers who fear typos, where if (a = null) would be an assignment and a valid statement... albeit with a warning in decent C compilers.

Here's an example of a badly implemented set of overloads, where the operand order matters, because null is convertible to both string and Test:

using System;

class Test
{
    public static bool operator ==(Test t, string x)
    {
        Console.WriteLine("Test == string");
        return false;
    }

    public static bool operator !=(Test t, string x)
    {
        Console.WriteLine("Test != string");
        return false;
    }

    public static bool operator ==(string x, Test t)
    {
        Console.WriteLine("string == Test");
        return false;
    }

    public static bool operator !=(string x, Test t)
    {
        Console.WriteLine("string != Test");
        return false;
    }

    static void Main(string[] args)
    {
        Test t = null;
        Console.WriteLine(t == null);
        Console.WriteLine(null == t);
    }
}

Now that the question has been updated, we can tell that's not the case... but the implementation could still be poor. For example, it could be written as:

public static bool operator == (UnityEngine.Object x, UnityEngine.Object y)
{
    // Awful implementation - do not use!
    return x.Equals(y);
}

In that case, it will fail with a NullReferenceException when x is null, but succeed if x is non-null but y is null (assuming Equals has been written properly).

Again though, I'd expect that the operator has been written properly, and that this isn't a problem.

people

See more on this question at Stackoverflow