为什么Unity3d C#中的语句允许非布尔对象? [英] Why do if statements in Unity3d C# allow objects that are not bools?

查看:86
本文介绍了为什么Unity3d C#中的语句允许非布尔对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准.NET 4.6编译器中,以下if语句不合法,您会收到编译器错误:CS0029无法将类型'UserQuery.TestClass'隐式转换为'bool'.这一切都很好,我知道.

void Main()
{
    TestClass foo = new TestClass();

    if(foo) 
    {
        "Huh. Who knew?".Dump();
    }
}


public class TestClass : Object
{
}

但是,在Unity3d中,这段代码是完全合法的,而且我看到了几个示例,甚至鼓励使用这种Javascript样式的空检查.

这是怎么回事,是否有可能在.NET的非Unity3d工作中利用它?

这是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.");
        }
    }
}

剧情变厚了!如果我尝试比较不是源于单声道行为的对象,则会收到预期的编译器警告.

public class TestClass
{
}
void Start ()
{
    var obj2 = new TestClass();
    if (obj2) // cast from TestClass to bool compiler error
    {

    }
}

由于程序员的缘故,我一路挖掘了Unity3D的对象(我忽略了它,因为我错误地认为它是.NET的实际对象.)

好奇的相关代码(在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);
}

解决方案

不要为此担心太多. if (obj)仅在Unity中合法,因为Unity中存在隐式的运算符重载.我相信这是在MonoBehaviourUnityEngine.Object类中完成的.

它看起来像这样:

public static implicit operator bool($classname$ other){
    return other != null;
}

Unity计划删除此功能,但决定反对此功能,因为它将破坏那里的每个Unity代码.您可以在此处了解更多信息>.

此功能的非常糟糕的一面是,它使得无法在编辑器中使用Null-Coalescing运算符.好的一面是,它简化了对null的检查./p>

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);
}

解决方案

Don't worry too much about this. The if (obj) is only legal in Unity because there is implicit operator overload in Unity. I believe that was done in the MonoBehaviour or UnityEngine.Object class.

It looks something like this:

public static implicit operator bool($classname$ other){
    return other != null;
}

Unity planed to remove this feature but decided to go against it because it would break every Unity code out there. You can read more about this here.

The very bad side of this feature is that it made it impossible to use the Null-Coalescing operator feature in the Editor. The good side is that it s simplifies checking for null.

这篇关于为什么Unity3d C#中的语句允许非布尔对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆