“或”之间的差异和“和” (|,||,&,&&) [英] Difference between "or" and "and" (|, ||, &, &&)

查看:108
本文介绍了“或”之间的差异和“和” (|,||,&,&&)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果string不是或不是null,请执行...,代码:

If string is not "" or is not null, do..., code:

if (string != "" || string != null) {}



但是这不起作用,我必须这样写:


but this is not working, i have to write this:

if (string != "" && string != null) {}



现在可行,但这对我来说很困惑,不是吗? (或),&意味着和?

我需要检查它们,或者在很多情况下甚至更多(5,6,......),它们是正确的:||或&&?


Now it works, but this is confusing for me, is it not | (or), & means and?
I need to check both of them or in many cases even more (5, 6,...) of them, what is the right one: || or &&?

推荐答案

我想你会在这里找到你所有的回答:

http://msdn.microsoft.com/en-us/library/2a723cdk%28v=vs.110%29 .aspx [ ^ ]



在这种情况下,文档总是很精彩:)
I think youll find all of your answeres here:
http://msdn.microsoft.com/en-us/library/2a723cdk%28v=vs.110%29.aspx[^]

The documentation is always wonderful in such a case :)


在C#中,不是或和和。运算符''|''和''&&'''是''|''和''&'',2)''||''和''&&'的优化变体;''只是布尔运算符,但''|''和''&''也是按位运算符。



为什么要使用布尔''|'''和''&''那么?它们确实是最好的避免,但是,为了完成布尔算法,它们是必需的。如果使用带有副作用的布尔函数,您将看到差异。在布尔表达式中使用这些函数非常不鼓励,但如果你这样做会怎么样?让我们看看:



In C#, there is not "or" and "and". The operators ''||'' and ''&&'' are 1) optimized variants of ''|'' and ''&'', 2) ''||'' and ''&&'' are only Boolean operators, but ''|'' and ''&'' are also bitwise ones.

Why use Boolean ''|'' and ''&'' then? They are really best avoided, but, for completeness of the Boolean arithmetic, they are needed. You will see the difference if you use a Boolean function with a side effect. Using such functions in a Boolean expression is highly discouraged, but what happens if you do it? Let''s see:

bool result = Left() || Right();





由于运算符''||''已经过优化,因此不会评估冗余操作数。如果 Left()返回true,则整个表达式将始终为true,因此表达式求值不会调用 Right()。如果右()有一些副作用,它会丢失(永远不会发生)。



同样在



As the operator ''||'' is optimized, it won''t evaluate the redundant operand. If Left() returns true, the whole expression will always be true, so the expression evaluation won''t call Right(). If Right() has some side effect, it would be lost (never happen).

Likewise in

bool result = Left() && Right();



如果 Left()返回false,则整个表达式始终为false,因此表达式求值不会打电话给 Right()。如果右()有一些副作用,它会丢失。



带''|''的表达式或''&''将始终评估所有操作数;这样,两个函数将始终被调用。同样的事情与 getters 导致任何副作用。



我希望现在一切都清楚了。



-SA
修复了明显的复制粘贴错误 - Matt Heffron [/ EDIT]


if Left() returns false, the whole expression is always false, so the expression evaluation won''t call Right(). If Right() has some side effect, it would be lost.

Expressions with ''|'' or ''&'' will always evaluate all the operands; this way, both functions will always be called. Same thing goes about properties with the getters which cause any side effect.

I hope everything is clear now.

—SA
Fixed apparent copy-paste errors - Matt Heffron[/EDIT]


如果字符串不是那么它一定是别的东西。

如果字符串不为null那么它必须是别的。



你说的是



如果字符串不是或者是OR以外的其他东西......



那么,如果它是它不是null而且如果它是null它不是。



当它令人困惑时,看看它和使用示例替换if的每个部分。所以,比方说字符串为空。



如果字符串!=|| string!= null

变为



如果为真|| false



= = true。



使用&&你说是



如果字符串不是而且它也是除了null以外的东西......换句话说,如果它既不是空也不是空。





(实际上有一个方法String.IsNullOrEmpty()你可以使用哪个有帮助!)





PS你应该首先检查null,作为||和&&运算符从左到右进行检查,因此当您检查的内容为null时,您可能会得到一个空引用异常。
If string is not "" then it must be something else.
If string is not null then it must be something else.

So you are saying

If string is something other than "" OR it is something other than null...

Well, if it is "" it is something other than null and if it is null it is something other than "".

When it is confusing, look at it and replace each part of the if using an example. So, say the string is null.

If string != "" || string != null
becomes

if true || false

which = true.

Using && you are saying

IF the string is something other than "" AND it is also something other than null ... in other words, if it is neither null nor empty.


(In fact there is a method String.IsNullOrEmpty() you can use which helps!)


P.S. You should check for null first, as the || and && operators do the checking left to right, so you may get a null reference exception when what you are checking is null.


这篇关于“或”之间的差异和“和” (|,||,&,&&)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆