C# 空合并 (??) 运算符的运算符优先级是什么?

发布于 2024-07-13 08:03:51 字数 455 浏览 7 评论 0原文

我刚刚尝试了以下操作,其想法是连接两个字符串,用空字符串替换空值。

string a="Hello";
string b=" World";

-- 调试(有趣的是?是打印,并不完全有助于可读性......)

 ? a ?? "" + b ?? "" 

-> “Hello”

正确的是:

? (a??"")+(b??"")
"Hello World"

我有点期待“Hello World”,或者如果 a 为空,则只是“World”。 显然,这与运算符优先级有关,可以通过括号来克服,是否有任何地方记录了这个新运算符的优先级顺序。

(意识到我可能应该使用 stringbuilder 或 String.Concat)

谢谢。

I've just tried the following, the idea being to concatenate the two strings, substituting an empty string for nulls.

string a="Hello";
string b=" World";

-- Debug (amusing that ? is print, doesn't exactly help readability...)

 ? a ?? "" + b ?? "" 

-> "Hello"

Correct is:

? (a??"")+(b??"")
"Hello World"

I was kind of expecting "Hello World", or just "World" if a is null. Obviously this is todo with operator precedence and can be overcome by brackets, is there anywhere that documents the order of precedence for this new operator.

(Realising that I should probably be using stringbuilder or String.Concat)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨笨の傻瓜 2024-07-20 08:03:51

除了您想要的优先级、ECMA 的规定、MS 规范的规定以及 csc 的实际用途之外,我还有一点建议:

不要这样做

我认为写起来更清楚:

string c = (a ?? "") + (b ?? "");

或者,考虑到字符串连接中的 null 最终只是一个空字符串,只需写:

string c = a + b;

编辑:关于记录的优先级,在 C# 3.0 规范 ( Word 文档)和 ECMA-334,另外比 ?? 绑定更紧密,而 ?? 比赋值绑定更紧密。 另一个答案中给出的 MSDN 链接是错误且奇怪的,IMO。 2008 年 7 月的页面上显示了一项更改,其中移动了条件运算符 - 但显然是错误的!

Aside from what you'd like the precedence to be, what it is according to ECMA, what it is according to the MS spec and what csc actually does, I have one bit of advice:

Don't do this.

I think it's much clearer to write:

string c = (a ?? "") + (b ?? "");

Alternatively, given that null in string concatenation ends up just being an empty string anyway, just write:

string c = a + b;

EDIT: Regarding the documented precedence, in both the C# 3.0 spec (Word document) and ECMA-334, addition binds tighter than ??, which binds tighter than assignment. The MSDN link given in another answer is just wrong and bizarre, IMO. There's a change shown on the page made in July 2008 which moved the conditional operator - but apparently incorrectly!

乖乖兔^ω^ 2024-07-20 08:03:51

切勿依赖运算符优先级。 始终明确指定您希望代码如何运行。 当您回到代码时,请为自己和他人一个忙。

(a ?? "") + (b ?? "")

这没有留下任何歧义的余地。 歧义是错误的滋生地。

Never rely on operator precedence. Always explicitly specify how you want your code to act. Do yourself and others a favour for when you come back to your code.

(a ?? "") + (b ?? "")

This leaves no room for ambiguity. Ambiguity is the breeding ground of bugs.

花桑 2024-07-20 08:03:51

运算符优先级记录在 MSDN 上。

但是,MSDN 上的优先级与 可下载的 C# 规范 也来自 Microsoft,以及 ECMA 规范。 这有点奇怪。

无论如何,正如 Jon Skeet 在他的回应中所说,最好不要依赖运算符的优先级,而是通过使用括号来明确。

The operator precedence is documented on MSDN.

However the precedence on MSDN contradicts the precedence in both the downloadable C# spec also from Microsoft, and the spec on ECMA. Which is a little odd.

Irrespective, as Jon Skeet said in his response, best not to rely on precedence of operators, but to be explicit through use of brackets.

2024-07-20 08:03:51

有趣的是 http://msdn.microsoft.com/en-us/library /6a71f45d.aspxhttp://en.csharp- online.net/ECMA-334:_14.2.1_Operator_precedence_and_associativity 为 ?? 提供不同的优先级。

msdn:

  1. 条件
  2. 赋值
  3. 空合并
  4. Lambda

ECMA:

  1. 空合并条件赋值
  2. 认为

msdn一定是错误的,考虑:

string a = null;
string b = a ?? "foo";
// What is b now?

It interesting that http://msdn.microsoft.com/en-us/library/6a71f45d.aspx and http://en.csharp-online.net/ECMA-334:_14.2.1_Operator_precedence_and_associativity give different precedence to ??.

msdn:

  1. Conditional
  2. Assignment
  3. Null-coalescing
  4. Lambda

ECMA:

  1. Null Coalescing
  2. Conditional
  3. Assignment

I think the msdn must be wrong, consider:

string a = null;
string b = a ?? "foo";
// What is b now?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文