&& .NET 中的操作

发布于 2024-08-30 06:05:49 字数 322 浏览 4 评论 0原文

在执行 && 时应优先选择以下两项中的哪一项对两个值进行运算。

 if (!StartTime.Equals(DateTime.MinValue) &&
    !CreationTime.Equals(DateTime.MinValue))

或者

     if (!(StartTime.Equals(DateTime.MinValue) && CreationTime.Equals(DateTime.MinValue))

两者有什么区别?

Which one out of the following two should be preferred while doing && operation on two values.

 if (!StartTime.Equals(DateTime.MinValue) &&
    !CreationTime.Equals(DateTime.MinValue))

Or

     if (!(StartTime.Equals(DateTime.MinValue) && CreationTime.Equals(DateTime.MinValue))

What is the difference between the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

轮廓§ 2024-09-06 06:05:49
(Not A)  AND  (Not B)

等于

Not (A And B)
(Not A)  AND  (Not B)

is NOT equal to

Not (A And B)
累赘 2024-09-06 06:05:49

我个人使用前者,我发现如果尽可能多的信息尽可能接近我需要的点,那么阅读会更容易。

例如,刚才我想知道你的问题是把表达式放在两行还是一行更好,直到我注意到关于 not 和括号的部分。

也就是说,只要它们的含义相同,而您的表达方式不同。

IE。

if (!a && !b)

不等于:

if (!(a && b))

相反:

if (!(a || b))

Personally I use the former, I find it easier to read if as much information as possible is as close to the point I need it as possible.

For instance, just now I was wondering if your question was whether it was better to put the expression on two lines or just one, until I noticed the part about the not and the parenthesis.

That is, provided they mean the same, which your expressions doesn't.

ie.

if (!a && !b)

is not the same as:

if (!(a && b))

Rather:

if (!(a || b))
虚拟世界 2024-09-06 06:05:49

嗯,这取决于你想要什么。它们都做不同的事情,并且在给定的上下文中任何一个都可能是正确的。

Well, it depends what you want. They both do different things, and either might be correct in the given context.

白首有我共你 2024-09-06 06:05:49

仅就格式而言,我更喜欢:

if(MyFirstExtremlyLongExpressionWhichBarelyFitsIntoALine
  && MySecondExtremlyLongExpressionWhichBarelyFitsIntoALine
  && MyThirdExtremlyLongExpressionWhichBarelyFitsIntoALine
) {
  // ...
}

但如果表达式确实又长又复杂,则应该定义临时变量以增强可读性:

bool condition1 = MyFirstExtremlyLongExpressionWhichBarelyFitsIntoALine;
bool condition2 = MySecondExtremlyLongExpressionWhichBarelyFitsIntoALine;
bool condition3 = MyThirdExtremlyLongExpressionWhichBarelyFitsIntoALine;

if(condition1 && condition2 && condition3) {
  // ...
}

如果您正在执行更复杂的布尔表达式,后者也阐明了您的意图:

if((!condition1 && !condition2) && condition3) {
  // ...
}

Regarding only the formatting, I prefere:

if(MyFirstExtremlyLongExpressionWhichBarelyFitsIntoALine
  && MySecondExtremlyLongExpressionWhichBarelyFitsIntoALine
  && MyThirdExtremlyLongExpressionWhichBarelyFitsIntoALine
) {
  // ...
}

But if the expressions are really long and compley, you should define temporary variables to enhance readability:

bool condition1 = MyFirstExtremlyLongExpressionWhichBarelyFitsIntoALine;
bool condition2 = MySecondExtremlyLongExpressionWhichBarelyFitsIntoALine;
bool condition3 = MyThirdExtremlyLongExpressionWhichBarelyFitsIntoALine;

if(condition1 && condition2 && condition3) {
  // ...
}

The latter also clarifies your intention, if you are doing more complex boolean expressions:

if((!condition1 && !condition2) && condition3) {
  // ...
}
心奴独伤 2024-09-06 06:05:49

第一个更具可读性,而可读性是这里最重要的事情。

第二个实际上与第一个陈述相同吗?不应该是||吗?

The first one is much more readable, and readability is the most important thing here.

Is the second one actually equivelent to the first statement? Shouldn't it be ||?

給妳壹絲溫柔 2024-09-06 06:05:49

如果输入值,您将看到

(!1 && !1) == (0 && 0) == 0
(!1 && !0) == (0 && 1) == 0

!(1 && 1) == !1 == 0
!(1 && 0) == !0 == 1

If you give put in the values you will see

(!1 && !1) == (0 && 0) == 0
(!1 && !0) == (0 && 1) == 0

!(1 && 1) == !1 == 0
!(1 && 0) == !0 == 1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文