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

為什么 .ToString() 在空字符串上導致空錯誤,當

Why does .ToString() on a null string cause a null error, when .ToString() works fine on a nullable int with null value?(為什么 .ToString() 在空字符串上導致空錯誤,當 .ToString() 在具有空值的可空整數上正常工作時
本文介紹了為什么 .ToString() 在空字符串上導致空錯誤,當 .ToString() 在具有空值的可空整數上正常工作時?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

selectedItem 有兩個字段:

  • int?_成本
  • string _serialNumber

在這個例子中,selectedItem_cost_serialNumber 都為 null.我正在通過它們的屬性閱讀 selectedItem 的字段,并用它們的值填充文本框,當...

In this example, _cost and _serialNumber of selectedItem are BOTH null. I am reading through the fields of selectedItem via their properties, and filling in textboxes with their values, when...

TextBox1.Text = selectedItem.Cost.ToString(); //no error
TextBox2.Text = selectedItem.SerialNumber.ToString(); //error

我知道 SerialNumber.ToString() 是多余的(因為它已經是一個字符串),但我不明白為什么會導致這個異常:

I understand that SerialNumber.ToString() is redundant (because it is already a string), but I don't understand why this causes this exception:

可空對象必須有值.

  • int?_cost 可以為空,并且沒有值,但它沒有給我例外.
  • string _serialNumber 可以為空,并且沒有值,但它確實給了我例外.
    • int? _cost is nullable, and does not have a value, yet it does not give me the exception.
    • string _serialNumber is nullable, and does not have a value, yet it does give me the exception.
    • 這個問題觸及它,這家伙本質上在問同樣的事情,但有沒有指定的答案,它也沒有解釋為什么可以為 null int?例如,我可以在可空 int 上使用 .ToString() 而不是在空字符串上使用嗎?

      This question touches on it, the guy is essentially asking the same thing, but there is no designated answer, and it also doesn't explain why a nullable int? For example, can I use .ToString() on a nullable int but not on a null string?

      推薦答案

      因為 string 類型的 null 真的指向空,所以內存中沒有任何對象.
      但是 int? type(nullable) 即使將值設置為 null 仍然指向某個對象.
      如果您閱讀 Jeffrey Richter 的CLR via C#"你會發現可空類型只是普通類型的外觀類,帶有一些封裝的邏輯,以便更方便地使用 DB null.

      Because string type's null really points to nothing, there isn't any object in memory.
      But int? type(nullable) even with value set to null still points to some object.
      If you read Jeffrey Richter's "CLR via C#" you'll find out that nullable type are just facade classes for common types with some incapsulated logics in order to make work with DB null more convenient.

      查看 msdn 以了解可空類型.

      Check msdn to learn about nullable types.

      這篇關于為什么 .ToString() 在空字符串上導致空錯誤,當 .ToString() 在具有空值的可空整數上正常工作時?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to check if String is null(如何檢查字符串是否為空)
Equals(item, null) or item == null(Equals(item, null) 或 item == null)
Overriding == operator. How to compare to null?(覆蓋 == 運算符.如何與空值進行比較?)
What does the question mark in member access mean in C#?(成員訪問中的問號在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運算符)
C# null coalescing operator equivalent for c++(C# 空合并運算符等效于 C++)
主站蜘蛛池模板: 欧美日韩国产精品一区二区 | 亚洲女优在线播放 | 国产福利资源 | 最新中文字幕第一页视频 | 在线91 | 天天综合久久网 | av网站免费看| 人人干人人干人人干 | 一级黄色毛片a | 中文字幕在线观看一区二区 | 乱码av午夜噜噜噜噜动漫 | 国产三区四区 | 特级a欧美做爰片毛片 | 免费观看www7722午夜电影 | 免费一级毛片 | 中文字幕一区二区三区不卡在线 | 国产高清在线 | 亚洲日本欧美 | 国产精品日日摸夜夜添夜夜av | 久久欧美精品 | 中文精品视频 | 狠狠操你 | 黑人巨大精品欧美一区二区免费 | 在线免费观看视频你懂的 | 91精品国产综合久久香蕉922 | 91精品国产91久久久久久密臀 | 中文字幕一区二区在线观看 | 精品久久av | 国产精品免费在线 | 亚洲伊人a | 成人在线免费视频 | 玩丰满女领导对白露脸hd | 国产9999精品 | 欧美日韩高清免费 | 久久精品欧美一区二区三区不卡 | 日韩中文字幕在线观看 | 91久久久久久久久久久 | 欧美日韩国产一区二区三区不卡 | 国产一区二区不卡 | 91免费小视频 | 黄色大片在线 |