C#null-coalescing(??)运算符的运算符优先级是什么?

时间:2022-01-22 11:46:52

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.

如果a为null,我有点期待“Hello World”,或者只是“World”。显然,这是运算符优先级的todo,可以通过括号来克服,是否存在记录此新运算符的优先顺序的任何位置。

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

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

Thanks.

4 个解决方案

#1


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:

除了你想要的优先级,ECMA是什么,根据MS规范是什么以及csc实际上做了什么,我有一点建议:

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:

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

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!

编辑:关于记录的优先顺序,在C#3.0规范(Word文档)和ECMA-334中,加法绑定比绑定更严格,绑定比绑定更严格。在另一个答案中给出的MSDN链接是错误的,奇怪的,IMO。 2008年7月页面上显示的更改移动了条件运算符 - 但显然不正确!

#2


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.

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

#3


The operator precedence is documented on MSDN.

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.

但是,MSDN的优先级与微软的可下载C#规范和ECMA规范的优先级相矛盾。这有点奇怪。

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

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

#4


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 ??.

有趣的是,http://msdn.microsoft.com/en-us/library/6a71f45d.aspx和http://en.csharp-online.net/ECMA-334:_14.2.1_Operator_precedence_and_associativity给出了不同的优先级。

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:

我认为msdn一定是错的,考虑一下:

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

#1


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:

除了你想要的优先级,ECMA是什么,根据MS规范是什么以及csc实际上做了什么,我有一点建议:

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:

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

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!

编辑:关于记录的优先顺序,在C#3.0规范(Word文档)和ECMA-334中,加法绑定比绑定更严格,绑定比绑定更严格。在另一个答案中给出的MSDN链接是错误的,奇怪的,IMO。 2008年7月页面上显示的更改移动了条件运算符 - 但显然不正确!

#2


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.

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

#3


The operator precedence is documented on MSDN.

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.

但是,MSDN的优先级与微软的可下载C#规范和ECMA规范的优先级相矛盾。这有点奇怪。

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

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

#4


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 ??.

有趣的是,http://msdn.microsoft.com/en-us/library/6a71f45d.aspx和http://en.csharp-online.net/ECMA-334:_14.2.1_Operator_precedence_and_associativity给出了不同的优先级。

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:

我认为msdn一定是错的,考虑一下:

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