我想知道是否可以修改检查对象的实例是否为空时返回的 bool 值,例如(我知道这是错误和不完整的,只是想给你一个引用):
主要:
SuperObject obj = new SuperObject();
if (obj == null) Console.WriteLine("It is null lol!");
super 对象:
public bool destroyed = false;
public static bool operator ==(SuperObject A, object B)
{
if (A != null && B == null && destroyed == true)
return true;
}
因此,如果检查表达式 (A == null) 并且 A 不为空但 A.destroyed 为 TRUE,它将返回 (A == null) 为 TRUE。
基本上我希望 (A == null) 在以下情况下为 TRUE: A 真的是 null OR A.destroyed = null;其他比较的默认值。
最佳答案
我建议改为这样做:
public static bool IsDestroyed(SuperObject a) {
return (a == null || a.destroyed);
}
你的方式会让新开发者感到困惑。
关于c# - 如果对象的实例为空,是否可以修改检查时返回的 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850686/