为什么我不能赋予null三元运营商为十进制? [英] Why can't I assign null to decimal with ternary operator?
本文介绍了为什么我不能赋予null三元运营商为十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么这是行不通的。
小数? COM pretAmount =!string.IsNullOrEmpty(txtLineCom pretAmt.Text)
? decimal.Parse(txtLineCom pretAmt.Text.Replace(,,))
: 空值;
解决方案
由于空
的类型为对象
(有效无类型),你需要将其分配到一个类型的对象。
这应该工作:
小数? COM pretAmount =!string.IsNullOrEmpty(txtLineCom pretAmt.Text)
? decimal.Parse(txtLineCom pretAmt.Text.Replace(,,))
:空;(十进制?)
或者这是一个好一点:
小数? COM pretAmount =!string.IsNullOrEmpty(txtLineCom pretAmt.Text)
? decimal.Parse(txtLineCom pretAmt.Text.Replace(,,))
:默认情况下(十进制);
下面是MSDN链接,默认关键字。
I can't understand why this won't work
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
: null;
解决方案
Because null
is of type object
(effectively untyped) and you need to assign it to a typed object.
This should work:
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
: (decimal?)null;
or this is a bit better:
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
: default(decimal?);
Here is the MSDN link for the default keyword.
这篇关于为什么我不能赋予null三元运营商为十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文