gpt4 book ai didi

c# - 重写 C# 条件语句问题

转载 作者:IT王子 更新时间:2023-10-29 04:51:53 26 4
gpt4 key购买 nike

我今天正在写一些代码,但有些地方没有按我预期的那样工作。

为什么即使条件的计算结果为 false,下面的代码仍会执行?

alt text http://img215.imageshack.us/img215/3011/agfewrf.gif

我尝试用大括号括起这两个条件,并调换它们的位置,但 EndedUsingApplication 甚至仍然执行。

编辑:

与||无关或 && 运算符。看看这个……

alt text

除非我发布罪魁祸首代码,否则没有人能从我的错误中吸取教训,所以就在这里。

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

这是工作代码......

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

问题似乎出在 != 运算符收到两个空值时。

最佳答案

你是否重载了 !=

关于c# - 重写 C# 条件语句问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com