c# - c# 吗??运算符(operator)短路?

标签 c# operators short-circuiting

在C#中使用??运算符时,如果被测试的值不为null,是否会短路?

例子:

string test = null;
string test2 = test ?? "Default";

string test3 = test2 ?? test.ToLower();

test3行是成功还是抛出空引用异常?

那么问题的另一种表达方式:右 watch 达 ??如果左手不为空,运算符得到评估?

最佳答案

是的,它在 C# Language Specification 中这样说(由我突出显示):

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.

关于c# - c# 吗??运算符(operator)短路?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5318912/

相关文章:

c# - ListView + POCO

c# - 如何在 web.config 之外安全地使用 ASP.NET 和 Azure 的凭据

c# - 上传图片并相应地在 gridview 中显示其缩略图

javascript - 这条线在做什么?到达长度 >>> 0

c++ - 为什么 VB.NET 和 C++ 对相同的表达式给出不同的结果?

linux - 为什么 bash 不会因短路命令序列中的错误而停止?

C# : how can I create User account remotely?

ruby-on-rails - Rails——&= 做什么?

c# - 在 C# 中对 boolean 值使用 &=

haskell - 如何使用 "either-side"和 "and"进行并行 "or"短路