简体   繁体   English

抛出Invalidcastexception,将null分配为可为空的十进制类型

[英]Invalidcastexception thrown assigning null to type nullable decimal

I put it back here as I am not dreaming. 我没有做梦就把它放回了这里。

Symptom: I am unable to assign null value to type decimal?. 症状:我无法分配空值来键入十进制? Assigning null throws exception. 分配null引发异常。

decimal? value = (decimal?) null;

This time I add a picture as evidence because apparently others do not believe my issue. 这次我添加图片作为证据,因为显然其他人不相信我的问题。

Problem persists. 问题仍然存在。 This is the full code I am not hiding anything. 这是完整的代码,我没有隐藏任何东西。 I recompiled, reloaded and restarted. 我重新编译,重新加载并重新启动。 I also expect this to be working. 我也希望这能奏效。 But it does not. 但事实并非如此。

在此处输入图片说明

Has anyone experienced similar? 有没有人经历过类似的经历?

Clean your solution and make sure that you debug your application in the DEBUG mode or have some debugging symbols from the current build. 清理您的解决方案,并确保您以DEBUG模式调试应用程序,或者从当前版本中获得一些调试符号。 The code that you are showing is not throwing the exception, but something else from what you didn't shown is throwing this exception. 您所显示的代码没有引发异常,但是您未显示的其他内容却引发了该异常。 For a prototype application, in up to date Visual Studio the sample code that you are showing works fine: 对于原型应用程序,在最新的Visual Studio中,您显示的示例代码可以正常工作:

在此处输入图片说明

As you can see, the (decimal?) cast of null is not required, so you can just remove it and recompile your application. 如您所见,不需要null的(decimal?)强制转换,因此您只需删除它并重新编译您的应用程序即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM