久久久久久久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 的方法調用)
主站蜘蛛池模板: 欧美国产视频 | 欧美久久一区 | 蜜臀久久 | 成年免费大片黄在线观看一级 | 国产高清无av久久 | 久久高清 | 精品一区二区三区在线观看 | 日本福利在线观看 | 红色av社区 | 国产精品一区二区在线播放 | 国产精品高清在线 | 国产精品久久久久久久久久久久久 | 欧美精品一区二区在线观看 | 精品国产精品三级精品av网址 | 国产在线中文字幕 | 看a级黄色毛片 | 91精品久久久久久久久99蜜臂 | 亚洲人成一区二区三区性色 | 国内精品一区二区三区 | 久久精品视频免费观看 | 午夜在线小视频 | 在线免费看黄 | 久久机热 | 国产一区二区三区免费观看视频 | 国产精品视频久久 | 精品国产乱码久久久久久影片 | a级大片免费观看 | 亚洲一区二区三区四区五区中文 | 国产精品乱码一二三区的特点 | 日韩在线视频观看 | 国产日韩欧美一区 | 亚洲成人在线视频播放 | 九九精品影院 | 欧美福利在线 | 欧美一区二区三区四区五区无卡码 | 九九综合九九 | 久久成人一区 | 国产成人精品一区二区在线 | 日本激情视频在线播放 | 夜夜爽99久久国产综合精品女不卡 | 在线不卡视频 |