久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C# 空合并 (??) 運算符的運算符優先級是什么?

What is the operator precedence of C# null-coalescing (??) operator?(C# 空合并 (??) 運算符的運算符優先級是什么?)
本文介紹了C# 空合并 (??) 運算符的運算符優先級是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我剛剛嘗試了以下方法,想法是連接兩個字符串,用空字符串替換空字符串.

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

->你好"

正確的是:

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

我有點期待Hello World",或者如果 a 為空,則只是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.

(意識到我可能應該使用 stringbuilder 或 String.Concat)

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

謝謝.

推薦答案

除了您希望喜歡的優先級之外,ECMA 規定的是什么,MS 規定的是什么規范和 csc 實際做什么,我有一點建議:

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:

不要這樣做.

我認為寫起來要清楚得多:

I think it's much clearer to write:

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

或者,鑒于字符串連接中的 null 無論如何最終只是一個空字符串,只需寫:

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

string c = a + b;

關于記錄的優先級,在 C# 3.0 規范(Word 文檔)和 ECMA-334,加法綁定比 ?? 更緊,后者比賦值綁定更緊.另一個答案中給出的 MSDN 鏈接是錯誤且奇怪的,IMO.2008 年 7 月的頁面上顯示的更改移動了條件運算符 - 但顯然是錯誤的!

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# 空合并 (??) 運算符的運算符優先級是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 亚洲免费视频一区 | 波多野结衣乳巨码无在线观看 | 久久久久久九九九九 | 日韩欧美中文在线 | a在线视频 | 国产亚洲区 | 黄色欧美视频 | 国产精品乱码一区二区视频 | 中文字幕第三页 | 日本亚洲欧美 | 亚洲三级视频 | 夜间福利视频 | 91亚洲视频| 国产免费网址 | 欧美日韩精品一区二区 | 国产成人免费观看 | 欧美自拍视频 | 欧美超碰在线 | 日本精品久久 | 欧美福利在线 | 可以免费看黄的网站 | 看黄色大片 | 五月综合色 | 欧美日韩免费视频 | 毛片视频免费 | 狠狠干天天 | 日韩精品久久久久久久酒店 | www.亚洲精品 | 中文字幕av久久爽一区 | www.啪啪 | 久久久婷婷 | 国产精品一区二区三区在线 | 欧美色婷婷| 久久久精品在线观看 | av在线天堂网 | 亚洲综合视频在线观看 | 一级片免费视频 | www.国产在线观看 | 色综合五月天 | 天天干天天操天天干 | 91午夜理伦私人影院 |