久久久久久久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 的方法調用)
主站蜘蛛池模板: 亚洲成人福利在线观看 | 亚洲精品一区二区三区中文字幕 | 亚洲精品大全 | 一级毛片,一级毛片 | 日韩1区2区 | 亚洲视频在线一区 | a级毛片免费高清视频 | 九九99精品 | 精品亚洲视频在线 | 九九热免费观看 | 久久久久高清 | 伊人中文字幕 | 欧美在线一区二区视频 | 午夜精品久久久久99蜜 | 日韩在线免费视频 | 国产色片在线 | 热久久性 | 国产精品污www一区二区三区 | 青青草av | 久久久涩| 一级做a爰片性色毛片16美国 | 黑人巨大精品欧美一区二区一视频 | 亚洲精品视频播放 | 日韩av手机在线观看 | 亚洲国产情侣自拍 | 剑来高清在线观看 | 91精品国产欧美一区二区 | 亚洲视频一区二区三区四区 | 亚洲最新在线 | a亚洲精品| 亚洲高清av在线 | 日本精品一区二区 | 久久成人久久 | 国产欧美一区二区三区日本久久久 | 国产毛片在线看 | 一区在线视频 | 日本韩国电影免费观看 | 日韩成人一区 | 精品中文视频 | 性生生活大片免费看视频 | 日韩精品免费一区二区在线观看 |