In the standard .NET 4.6 compiler, the following if statement is not legal, you will get the compiler error: CS0029 Cannot implicitly convert type 'UserQuery.TestClass' to 'bool'. This is all well and good, and I understand this.
void Main()
{
TestClass foo = new TestClass();
if(foo)
{
"Huh. Who knew?".Dump();
}
}
public class TestClass : Object
{
}
However, in Unity3d this code is perfectly legal and I have seen several examples even encouraging this Javascript style of null checking.
What's going on here and is it possible to leverage this in my non-Unity3d work in .NET?
Here is an example of legal code in Unity3D:
using UnityEngine;
public class SampleIfBehavior : MonoBehaviour
{
// Use this for initialization
void Start ()
{
var obj = new SampleIfBehavior();
if (obj)
{
Console.Write("It wasn't null.");
}
}
}
The plot thickens! If I attempt to compare an object that doesn't derive from mono behavior, I get the expected compiler warning.
public class TestClass
{
}
void Start ()
{
var obj2 = new TestClass();
if (obj2) // cast from TestClass to bool compiler error
{
}
}
Thanks to Programmer, I dug down all the way to Unity3D's Object (I overlooked it because I mistakenly assumed it was .NET's actual object.)
Relevant code for the curious (in UnityEngine.Object):
public static implicit operator bool(Object exists)
{
return !Object.CompareBaseObjects(exists, (Object) null);
}
public static bool operator ==(Object x, Object y)
{
return Object.CompareBaseObjects(x, y);
}
public static bool operator !=(Object x, Object y)
{
return !Object.CompareBaseObjects(x, y);
}