这让我很困惑。也许我现在太累了。
Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
Rectangle cropArea = inputArea == null ? rectangle : inputArea.Value;
if (inputArea == null)
cropArea = rectangle;
inputArea 是一个可为 null 的矩形,在我的特定情况下为 null。
前两个语句生成一个初始化为 0 的 cropArea。但是,第二个语句根据图像的宽度和高度生成正确的 cropArea。我对条件运算符有什么误解吗?似乎当 inputArea = null 时它不返回矩形?使用值类型时有什么怪癖吗?
编辑:好吧,我应该先尝试这个:重新启动 VS。调试器似乎对我撒了谎,或者什么的。无论如何,现在工作。谢谢。
最佳答案
这似乎是 Visual Studio Debug模式中的一个讨厌的错误,它在愚弄您:
现在 F10 跨过这条线,你会得到:
在控制台上打印正确的值。
卧槽
关于c# - .Net 中条件运算符的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3594620/