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

為什么連接空字符串有效但調用“null.ToString()&

Why is it Valid to Concatenate Null Strings but not to Call quot;null.ToString()quot;?(為什么連接空字符串有效但調用“null.ToString()無效?)
本文介紹了為什么連接空字符串有效但調用“null.ToString()"無效?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是有效的 C# 代碼

This is valid C# code

var bob = "abc" + null + null + null + "123";  // abc123

這不是有效的 C# 代碼

This is not valid C# code

var wtf = null.ToString(); // compiler error

為什么第一條語句有效?

Why is the first statement valid?

推薦答案

第一個工作的原因:

來自 MSDN:

在字符串連接操作中,C# 編譯器將空字符串視為空字符串,但不會轉換原始空字符串的值.

In string concatenation operations,the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string.

有關的更多信息+ 二元運算符:

當一個或兩個操作數都是字符串類型時,二元 + 運算符執行字符串連接.

The binary + operator performs string concatenation when one or both operands are of type string.

如果字符串連接的操作數為空,則替換為空字符串.否則,通過調用從類型對象繼承的虛擬 ToString 方法,將任何非字符串參數轉換為其字符串表示.

If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object.

如果 ToString 返回 null,則替換為空字符串.

If ToString returns null, an empty string is substituted.

第二個錯誤的原因是:

null(C# 參考) -null 關鍵字是表示空引用的文字,不引用任何對象.null 是引用類型變量的默認值.

null (C# Reference) - The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables.

這篇關于為什么連接空字符串有效但調用“null.ToString()"無效?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的方法調用)
主站蜘蛛池模板: 超碰免费97 | 亚洲福利专区 | 人人爽夜夜爽 | 日韩精品一区二区在线 | 久久精品视频网 | www.色中色| 成人在线免费看 | 黄色a网站 | 亚洲人成免费 | 欧美a级成人淫片免费看 | 欧美激情一区二区 | 亚洲永久免费视频 | 国产一区在线视频 | 欧美www | 中文字幕99| 色婷婷丁香 | 亚洲日本天堂 | 黑人巨大猛烈捣出白浆 | 天天碰天天操 | 免费黄色片视频 | 黄色片在线观看视频 | 免费一区二区 | 欧美亚洲视频 | 成人网页 | 国内精品久久久久久久久 | 中文字幕一区在线观看 | 神马午夜我不卡 | 亚洲乱码国产乱码精品精软件 | 国产三级久久 | 欧美视频在线一区 | 欧美日韩在线视频观看 | 日韩中文字幕一区二区三区 | 国产片一区二区 | 九九精品在线观看 | 日韩字幕| 97色婷婷 | 曰韩av| 啪啪毛片 | 视频一区二区三区在线观看 | 亚洲欧美乱综合图片区小说区 | 免费观看毛片 |