急于操作和短路操作之间的差异? (| versu ||和与放大器; versu和放大器;&安培;) [英] Difference between eager operation and short-circuit operation? (| versu || and & versu &&)

查看:154
本文介绍了急于操作和短路操作之间的差异? (| versu ||和与放大器; versu和放大器;&安培;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(仍然)学习C# - 我想我明白和放大器之间的区别; &放大器; &功放;&安培; 以及 | &放大器; || ...

I am (still) learning C# - and I thought I understood the difference between & & && as well as | & ||...

不过,只是读另一位导游后,很显然我不明白这一点。

However, after just reading another guide, it is clear I don't get it.

我写了一个小真值表正如我认为,他们返回相同的。从我已阅读,采用双层符号听起来像是一个出色的解决方案,但我上的区别有点迷茫,不知道是否有人能解释一下/给出为什么/时,你会使用一个,而不是其他的例子 - 我试着阅读MSDN的例子,但它留给我比当我开始更糊涂!

I wrote a little truth table and as I thought, they return the same. From what I have read, using double symbols sounds like a superior solution, but I am a little confused on the difference and was wondering if anyone could please explain/give an example why/when you would use one instead of the other - I tried reading the MSDN example, but it left me more confused than when I started!

(而且,如果有人能拿出一个更好的标题,随意改变它...非常尴尬写一个!)

(And, if anyone can come up with a better title, feel free to change it... very awkward to write one!)

推荐答案

&放大器;可以使用两种不同的方法:按位和和逻辑与

& can be used in two different ways: bitwise "and" and logical "and"

逻辑安培之间的差额;并与功放;&安培;是仅在情况下,你使用&安培;中,第二前pression进行了评价,即使第一前pression已经假。如果你想在循环初始化两个变量,这可能(例如)是有趣的:

The difference between logical & and && is only that in case you use &, the second expression is also evaluated, even if the first expression was already false. This may (for example) be interesting if you want to initialize two variables in the loop:

if ((first = (i == 7)) & (second = (j == 10))) { //do something }

如果您使用此语法,第一和第二总会有一个值,如果你使用

if you use this syntax, first and second will always have a value, if you use

if ((first = (i == 7)) && (second = (j == 10))) { //do something }

它可以是只具有第一后的评价值。

it may be that only first has a value after the evaluation.

有相同|和||:在您使用的情况下,|,无论是前pressions的一直被运用,如果你用||它可以是只在第一个前pression被计算,这将是该情况下,如果第一前pression为真

It is the same for | and ||: In case you use |, both of the expressions are always evaluated, if you use || it may be that only the first expression is evaluated, which would be the case if the first expression is true.

在另一方面,在其它应用&安培;&安培;可能是更好的选择。如果为mynumber的类型是诠释?中,你可以有像

In contrast, in other applications && can be the better choice. If myNumber is of type int?, you could have something like

if (myNumber != null && myNumber.Value == 7)

和这只能评估为mynumber!= NULL 首先,它只会计算第二个前pression,如果空检查是好的。

and this would only evaluate myNumber != null at first, and it would only evaluate the second expression, if the null check was okay.

if (myNumber != null & myNumber.Value == 7)

将与一个NullPointerException异常的第二个前pression在评估过程中完成,如果为mynumber为空。因此,你可以使用和放大器;&安培;在这方面

would finish with a NullPointerException during the evaluation of the second expression, if myNumber was null. Therefore, you would use && in this context.

这篇关于急于操作和短路操作之间的差异? (| versu ||和与放大器; versu和放大器;&安培;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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