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

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

问题描述

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

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
{
}

然而,在 Unity3d 中,这段代码是完全合法的,我已经看到几个例子甚至鼓励这种 Javascript 风格的空检查.

However, in Unity3d this code is perfectly legal and I have seen several examples even encouraging this Javascript style of null checking.

这里发生了什么,是否可以在 .NET 中的非 Unity3d 工作中利用它?

What's going on here and is it possible to leverage this in my non-Unity3d work in .NET?

以下是 Unity3D 中的合法代码示例:

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
    {

    }
}

感谢程序员,我一直挖掘到 Unity3D 的对象(我忽略了它,因为我错误地认为它是 .NET 的实际对象.)

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.)

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

推荐答案

不要太担心这个.if (obj) 仅在 Unity 中合法,因为 Unity 中存在隐式运算符重载.它是在 UnityEngine.Object 类中完成的.

Don't worry too much about this. The if (obj) is only legal in Unity because there is an implicit operator overload in Unity. It is done in the UnityEngine.Object class.

看起来像这样:

  public static implicit operator bool(Object obj)
     {
       return (obj != null);
     }

Unity 计划删除此功能,但决定反对它,因为它会破坏那里的所有 Unity 代码.您可以在此处阅读更多相关信息.

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.

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

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# 中的 if 语句允许不是 bool 的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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