我今天写了一些代码,有些东西没有像我预期的那样工作.
为什么即使条件应该评估为false,下面的代码也会执行?
替代文字http://img215.imageshack.us/img215/3011/agfewrf.gif
我已经尝试围绕这两个条件,并切换它们的位置,但EndedUsingApplication甚至仍然执行.
编辑:
它与||无关 或&&运营商.看这个...
alt text http://img20.imageshack.us/img20/6655/aaaaaal.gif
没有人可以从我的错误中吸取教训,除非我发布了罪魁祸首代码,所以在这里.
public static bool operator ==(ActiveApplication a, ActiveApplication b)
{
if ((object)a == null || (object)b == null)
return false;
return a.process_name == b.process_name && a.window_title == b.window_title;
}
public static bool operator !=(ActiveApplication a, ActiveApplication b)
{
return a == b ? false : true;
}
Run Code Online (Sandbox Code Playgroud)
这是工作代码......
public static bool operator ==(ActiveApplication a, ActiveApplication b)
{
// Casting to object class prevents this comparison operator being executed
// again and causing an infinite loop (which I think .NET detects and stops
// but it would still be a huge hole in the logic.
if ((object)a == null && (object)b == null)
return true;
if ((object)a == null ^ (object)b == null)
return false;
return a.process_name == b.process_name && a.window_title == b.window_title;
}
public static bool operator !=(ActiveApplication a, ActiveApplication b)
{
return a == b ? false : true;
}
Run Code Online (Sandbox Code Playgroud)
问题似乎是!=运算符收到两个空值.