Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Unity started shipping a newer compiler which comes with some new warnings. I noticed one today that flags this situation:

IInterface thing1 = GetSomeInterfaceRef();

// Thing2 implements IInterface, and overloads operator==
Thing2 thing2 = GetThing2();

bool thingsAreTheSame = thing1 == thing2;

Since the overloaded operator is statically resolved, and defined as bool operator==(Thing2 a, Thing2 b), it won't be used and fall back to reference equality instead.

This is a useful warning, because I've seen this trip people up.

Except it's practically useless in Unity code because its base UnityEngine.Object class overloads operator==, and you wind up with mostly false positives. You can avoid lots of false positives if you comb through all of these and replace x == y with object.ReferenceEquals(x, y). Yes, I did that, and it made me grumpy and prompted me to write this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment