gpt4 book ai didi

c# - 如果左操作数为??运算符不为空,正确的操作数是否被评估?

转载 作者:可可西里 更新时间:2023-11-01 03:03:53 25 4
gpt4 key购买 nike

我正在考虑在 C# 中使用 ?? 运算符(空合并运算符)。但是 documentation在 MSDN 上的访问量有限。

我的问题:如果左边的操作数不为空,右边的操作数会被计算吗?

最佳答案

一如既往,C# 规范是此类事情的最佳去处。

来自 C# 5 specification 的第 7.13 节(强调我的):

A null coalescing expression of the form a ?? b requires a to be of a nullable type or reference type. If a is non-null, the result of a ?? b is a; otherwise, the result is b. The operation evaluates b only if a is null.

有关何时执行任何转换以及确切行为的更多详细信息,但这是您提出问题的要点。还值得注意的是,空合并运算符是右结合的,所以 a ?? ?? c 被评估为 a ?? (b ?? c)... 这意味着它只会评估 c 如果 both ab 为空。

关于c# - 如果左操作数为??运算符不为空,正确的操作数是否被评估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26024328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com