1. <small id='dsseN'></small><noframes id='dsseN'>

      <tfoot id='dsseN'></tfoot>

      • <bdo id='dsseN'></bdo><ul id='dsseN'></ul>

      <i id='dsseN'><tr id='dsseN'><dt id='dsseN'><q id='dsseN'><span id='dsseN'><b id='dsseN'><form id='dsseN'><ins id='dsseN'></ins><ul id='dsseN'></ul><sub id='dsseN'></sub></form><legend id='dsseN'></legend><bdo id='dsseN'><pre id='dsseN'><center id='dsseN'></center></pre></bdo></b><th id='dsseN'></th></span></q></dt></tr></i><div id='dsseN'><tfoot id='dsseN'></tfoot><dl id='dsseN'><fieldset id='dsseN'></fieldset></dl></div>
      <legend id='dsseN'><style id='dsseN'><dir id='dsseN'><q id='dsseN'></q></dir></style></legend>

      为什么空字符串上的 .ToString() 会导致空错误,而 .ToString() 在具有空值的可空 int 上工作正

      时间:2023-07-25

      <legend id='qKDfB'><style id='qKDfB'><dir id='qKDfB'><q id='qKDfB'></q></dir></style></legend>

              <bdo id='qKDfB'></bdo><ul id='qKDfB'></ul>
                <tbody id='qKDfB'></tbody>
            • <tfoot id='qKDfB'></tfoot>
            • <i id='qKDfB'><tr id='qKDfB'><dt id='qKDfB'><q id='qKDfB'><span id='qKDfB'><b id='qKDfB'><form id='qKDfB'><ins id='qKDfB'></ins><ul id='qKDfB'></ul><sub id='qKDfB'></sub></form><legend id='qKDfB'></legend><bdo id='qKDfB'><pre id='qKDfB'><center id='qKDfB'></center></pre></bdo></b><th id='qKDfB'></th></span></q></dt></tr></i><div id='qKDfB'><tfoot id='qKDfB'></tfoot><dl id='qKDfB'><fieldset id='qKDfB'></fieldset></dl></div>

                <small id='qKDfB'></small><noframes id='qKDfB'>

                本文介绍了为什么空字符串上的 .ToString() 会导致空错误,而 .ToString() 在具有空值的可空 int 上工作正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                selectedItem 有两个字段:

                • <代码>int?_成本
                • 字符串_serialNumber

                在此示例中,selectedItem_cost_serialNumber 均为空.我正在通过它们的属性阅读 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:

                可空对象必须有一个值.

                Nullable object must have a value.

                • <代码>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.
                  • 这个 question 涉及到它,这家伙本质上是在问同样的事情,但是那里是没有指定的答案,它也没有解释为什么可以为空的 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() 在具有空值的可空 int 上工作正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:你如何在 C# 中做 *integer* 幂运算? 下一篇:如何在 C# 中将小数转换为 int?

                相关文章

                  <tfoot id='J3lYF'></tfoot>

                1. <legend id='J3lYF'><style id='J3lYF'><dir id='J3lYF'><q id='J3lYF'></q></dir></style></legend>
                      <bdo id='J3lYF'></bdo><ul id='J3lYF'></ul>

                    <i id='J3lYF'><tr id='J3lYF'><dt id='J3lYF'><q id='J3lYF'><span id='J3lYF'><b id='J3lYF'><form id='J3lYF'><ins id='J3lYF'></ins><ul id='J3lYF'></ul><sub id='J3lYF'></sub></form><legend id='J3lYF'></legend><bdo id='J3lYF'><pre id='J3lYF'><center id='J3lYF'></center></pre></bdo></b><th id='J3lYF'></th></span></q></dt></tr></i><div id='J3lYF'><tfoot id='J3lYF'></tfoot><dl id='J3lYF'><fieldset id='J3lYF'></fieldset></dl></div>
                  1. <small id='J3lYF'></small><noframes id='J3lYF'>