- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
假设 myObj 为空。这样写安全吗?
if(myObj != null && myObj.SomeString != null)
我知道有些语言不会执行第二个表达式,因为在执行第二部分之前 && 的计算结果为 false。
最佳答案
是的。在 C# 中,&&
和 ||
是短路的,因此仅当左侧尚未确定结果时才评估右侧。另一方面,运算符 &
和 |
不会短路,并且始终对两侧求值。
规范说:
The
&&
and||
operators are called the conditional logical operators. They are also called the “shortcircuiting” logical operators.
...
The operationx && y
corresponds to the operationx & y
, except thaty
is evaluated only ifx
istrue
...
The operationx && y
is evaluated as(bool)x ? (bool)y : false
. In other words,x
is first evaluated and converted to typebool
. Then, ifx
istrue
,y
is evaluated and converted to typebool
, and this becomes the result of the operation. Otherwise, the result of the operation isfalse
.
(C# 语言规范版本 4.0 - 7.12 条件逻辑运算符)
&&
和 ||
的一个有趣的属性是它们短路,即使它们不对 bool 操作,但是用户重载操作符的类型 &
或 |
以及 true
和 false
运算符。
The operation
x && y
is evaluated asT.false((T)x) ? (T)x : T.&((T)x, y)
, whereT.false((T)x)
is an invocation of theoperator false
declared inT
, andT.&((T)x
, y) is an invocation of the selectedoperator &
. In addition, the value (T)x shall only be evaluated once.In other words,
x
is first evaluated and converted to typeT
andoperator false
is invoked on the result to determine ifx
is definitelyfalse
.
Then, ifx
is definitelyfalse
, the result of the operation is the value previously computed forx
converted to typeT
.
Otherwise,y
is evaluated, and the selected operator&
is invoked on the value previously computed forx
converted to typeT
and the value computed fory
to produce the result of the operation.
(C# 语言规范版本 4.0 - 7.12.2 用户定义的条件逻辑运算符)
关于c# - 在 .NET 中依赖 && 短路安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820610/
考虑 function foo(x) x isa Bar || throw(ArgumentError("x is not a Int64...")) dosomething(x) e
这个问题在这里已经有了答案: AndAlso/OrElse in VBA (8 个回答) 7年前关闭。 VBA 不会短路 VBA 不支持短路 - 显然是因为它只有按位 And/Or/Not 等操作。来
这个问题在这里已经有了答案: Java operator precedence guidelines (6 个答案) 关闭 7 年前。 你好,我有一个关于 java 中的优先级表的问题。它说 &&
a &= foo(); 只是被 C++ 中的 a = a && foo(); 取代还是还有更多内容? 我关心的是 foo() 的计算。如果 a 为假,它会被调用吗? 最佳答案 && 是否有嵌入式短路:
我正在尝试注释以下代码。 当同时定义了 zone 和 zones 时,或者当定义了 file 时(但不是同时定义了两者),该函数将起作用: def get_file(zone: str, zones:
给定远程beam.smp服务的已知TCP端口和名称,以及已知的cookie,是否可以短路Erlang分发协议(protocol)的Erlang端口映射器守护进程握手阶段并直接建立Erlang shel
我正在尝试注释以下代码。 当同时定义了 zone 和 zones 时,或者当定义了 file 时(但不是同时定义了两者),该函数将起作用: def get_file(zone: str, zones:
我需要从第二个表中查询数据,但前提是满足主表中的一组罕见条件: SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM ta
如标题所述,我想将 onchange 短路(在选择内部)。我有以下代码,但我只想发送一个硬编码值而不显示选择框。 "> [编辑] 对不起,我不清楚。我什至不想提供
我是 C++ 的新手,很好奇编译器如何处理 bool 值的惰性求值。例如, if(A == 1 || B == 2){...} 如果 A 确实等于 1,那么 B==2 部分是否曾经计算过? 最佳答案
我在尝试让这个 linq 语句起作用时遇到了一些麻烦。我正在使用 linq 查询设置搜索。我想要做的是,如果搜索为空或为空,让它忽略过滤的那一部分。所以我设置了很多 where 子句来缩短 where
这个问题在这里已经有了答案: How to short-circuit a reduce() operation on a Stream? (4 个答案) 关闭 4 年前。 在做项目的时候写了这行,
首先,我不是 Bash 专家。几个月前我发现,如果我同时使用 && 和 || 短路运算符和花括号,那么如果第一个语句以真实值退出,如果 true block 中的最后一条语句退出非零,那么 fail
我有一个使用逻辑 OR 运算符的 NSPredicate,如下所示: NSPredicate(format: "activeFrom == NULL OR %@ >= activeFrom", som
我是 C++ 的新手,很好奇编译器如何处理 bool 值的惰性求值。例如, if(A == 1 || B == 2){...} 如果 A 确实等于 1,那么 B==2 部分是否曾经计算过? 最佳答案
我需要从第二个表中查询数据,但前提是满足主表中的一组罕见条件: SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM ta
我遇到了这段代码: 我想我们可以归纳为: 然后我找到了这个 article on short circuits和另一个 more focused one一旦我知道他们叫什么。但是,我还是不明白。
据我了解和阅读,您可以在 if 语句(&& 或 ||)中使用短路,以使第二个条件不触发。如果您希望两个条件都触发,您可以使用单个操作数(& 或 |)。 假设我有如下内联 if 语句: var test
在玩弄编译时字符串(char 的可变参数列表)操作时,我需要实现一种方法来检查编译时字符串是否包含另一个(较小的)编译时字符串。 这是我的第一次尝试: template struct Contains
如果我有以下... a OrElse b ... 并且 a 是 真那么显然 b 永远不会被评估。但是如果我添加一个 Or ,然后呢? a OrElse b Or c c 是否/应该得到评估?如果我放入
我是一名优秀的程序员,十分优秀!