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

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

    • <bdo id='Uu5Ej'></bdo><ul id='Uu5Ej'></ul>
    1. <legend id='Uu5Ej'><style id='Uu5Ej'><dir id='Uu5Ej'><q id='Uu5Ej'></q></dir></style></legend>
    2. <tfoot id='Uu5Ej'></tfoot>
    3. 为什么我不能将 int 拆箱为小数?

      时间:2023-07-25

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

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

                <tfoot id='pCQjy'></tfoot>
                本文介绍了为什么我不能将 int 拆箱为小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 IDataRecord 阅读器,我正在从中检索小数点,如下所示:

                I have an IDataRecord reader that I'm retrieving a decimal from as follows:

                decimal d = (decimal)reader[0];
                

                由于某种原因,这会引发一个无效的转换异常,指出指定的转换无效".

                For some reason this throws an invalid cast exception saying that the "Specified cast is not valid."

                当我执行 reader[0].GetType() 时,它告诉我它是一个 Int32.据我所知,这应该不是问题......

                When I do reader[0].GetType() it tells me that it is an Int32. As far as I know, this shouldn't be a problem....

                我已经通过这个片段测试了它,效果很好.

                I've tested this out by this snippet which works just fine.

                int i = 3750;
                decimal d = (decimal)i;
                

                这让我摸不着头脑,想知道为什么它无法将阅读器中包含的 int 拆箱为小数.

                This has left me scratching my head wondering why it is failing to unbox the int contained in the reader as a decimal.

                有谁知道为什么会发生这种情况?有什么我遗漏的微妙之处吗?

                Does anyone know why this might be occurring? Is there something subtle I'm missing?

                推荐答案

                您只能将值类型拆箱为其原始类型(以及该类型的可空版本).

                You can only unbox a value type to its original type (and the nullable version of that type).

                顺便说一句,这是有效的(只是你的两行版本的简写):

                By the way, this is valid (just a shorthand for your two line version):

                object i = 4;
                decimal d = (decimal)(int)i; // works even w/o decimal as it's a widening conversion
                

                对于这背后的原因,请阅读此 Eric Lippert 的博客条目:Representation and身份

                For the reason behind this read this Eric Lippert's blog entry: Representation and Identity

                就我个人而言,我将通过 cast 语法所做的事情分为四种不同类型的操作(它们都有不同的 IL 指令):

                Personally, I categorize things done by cast syntax into four different types of operation (they all have different IL instructions):

                1. 装箱(box IL指令)和拆箱(unbox IL指令)
                2. 通过继承层次进行强制转换(如C++中的dynamic_cast,使用castclass IL指令进行验证)
                3. 原始类型之间的转换(如 C++ 中的 static_cast,有大量 IL 指令用于原始类型之间的不同类型转换)
                4. 调用用户定义的转换运算符(在 IL 级别,它们只是对适当 op_XXX 方法的方法调用).
                1. Boxing (box IL instruction) and unboxing (unbox IL instruction)
                2. Casting through the inhertiance hierarchy (like dynamic_cast<Type> in C++, uses castclass IL instruction to verify)
                3. Casting between primitive types (like static_cast<Type> in C++, there are plenty of IL instructions for different types of casts between primitive types)
                4. Calling user defined conversion operators (at the IL level they are just method calls to the appropriate op_XXX method).

                这篇关于为什么我不能将 int 拆箱为小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将int转换为字符串? 下一篇:如何优雅地检查一个数字是否在一个范围内?

                相关文章

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

                  • <bdo id='iEGgp'></bdo><ul id='iEGgp'></ul>
                  <legend id='iEGgp'><style id='iEGgp'><dir id='iEGgp'><q id='iEGgp'></q></dir></style></legend>

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