最近我有一個奇怪的錯誤,我在連接字符串與int?
,然後加入之後另一個字符串。奇怪的運算符優先級與?? (空合併運算符)
我的代碼基本上是這樣的等價物:
int? x=10;
string s = "foo" + x ?? 0 + "bar";
令人吃驚的是,這將運行並沒有出現警告或不兼容的類型錯誤編譯,也將這樣的:
int? x=10;
string s = "foo" + x ?? "0" + "bar";
然後這導致意外類型不兼容性錯誤:
int? x=10;
string s = "foo" + x ?? 0 + 12;
就像這樣簡單例如:
int? x=10;
string s = "foo" + x ?? 0;
有人可以解釋這是如何對我有用嗎?
一個相關的問題:http://stackoverflow.com/questions/3218140/null-coalescing-operator-and-lambda-expression/3218268#3218268 – 2010-07-15 21:38:23
這裏是一個鏈接,不會使我的答案發光... http://stackoverflow.com/questions/3218140/null-coalescing-operator-and-lambda-expression – ChaosPandion 2010-07-15 22:17:18