Skip to main content
 首页 » 编程设计

c#之如果对象的实例为空,是否可以修改检查时返回的 bool 值

2024年02月23日15hnrainll

我想知道是否可以修改检查对象的实例是否为空时返回的 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); 
} 

你的方式会让新开发者感到困惑。