&& .NET 中的操作
在执行 && 时应优先选择以下两项中的哪一项对两个值进行运算。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不等于
is NOT equal to
我个人使用前者,我发现如果尽可能多的信息尽可能接近我需要的点,那么阅读会更容易。
例如,刚才我想知道你的问题是把表达式放在两行还是一行更好,直到我注意到关于 not 和括号的部分。
也就是说,只要它们的含义相同,而您的表达方式不同。
IE。
不等于:
相反:
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.
is not the same as:
Rather:
嗯,这取决于你想要什么。它们都做不同的事情,并且在给定的上下文中任何一个都可能是正确的。
Well, it depends what you want. They both do different things, and either might be correct in the given context.
仅就格式而言,我更喜欢:
但如果表达式确实又长又复杂,则应该定义临时变量以增强可读性:
如果您正在执行更复杂的布尔表达式,后者也阐明了您的意图:
Regarding only the formatting, I prefere:
But if the expressions are really long and compley, you should define temporary variables to enhance readability:
The latter also clarifies your intention, if you are doing more complex boolean expressions:
第一个更具可读性,而可读性是这里最重要的事情。
第二个实际上与第一个陈述相同吗?不应该是||吗?
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 ||?
如果输入值,您将看到
(!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