C# 为什么我不能使用三元运算符将 null 分配给十进制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9308443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I assign null to decimal with ternary operator?
提问by Zo Has
I can't understand why this won't work
我不明白为什么这行不通
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
: null;
采纳答案by slugster
Because nullis of type object(effectively untyped) and you need to assign it to a typed object.
因为null是类型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 defaultkeyword.
这是默认关键字的 MSDN 链接。
回答by Jakub Konecki
You need to cast the first part to decimal?
您需要将第一部分转换为 decimal?
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
? (decimal?)decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
: null;
回答by vc 74
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) ?
decimal.Parse(txtLineCompRetAmt.Text.Replace(",","")) :
(decimal?)null;
回答by juergen d
Try this:
尝试这个:
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) ?
decimal.Parse(txtLineCompRetAmt.Text.Replace(",", "")) :
(decimal?) null;
The problem is that the compiler does not know what type nullhas. So you can just cast it to decimal?
问题是编译器不知道有什么类型null。所以你可以把它投射到decimal?
回答by Thomas Levesque
Because the compiler can't infer the best type from the operands of the conditional operator.
因为编译器无法从条件运算符的操作数中推断出最佳类型。
When you write condition ? a : b, there must be an implicit conversion from the type of ato the type of b, or from the type of bto the type of a. The compiler will then infer the type of the whole expression as the target type of this conversion. The fact that you assign it to a variable of type decimal?is never considered by the compiler. In your case, the types of aand bare decimaland some unknown reference or nullable type. The compiler can't guess what you mean, so you need to help it:
编写时condition ? a : b,必须有从类型a到类型的隐式转换b,或从类型b到类型的隐式转换a。然后编译器将整个表达式的类型推断为该转换的目标类型。decimal?编译器从不考虑将它分配给类型变量的事实。在你的情况下,该类型的a和b是decimal和一些未知的引用或可空类型。编译器猜不到你的意思,所以你需要帮助它:
decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
: default(decimal?);
回答by Ebad Masood
Don't use decimal.Parse.
不要使用decimal.Parse.
Convert.ToDecimalwill return 0 if it is given a null string. decimal.Parsewill throw an ArgumentNullException if the string you want to parse is null.
Convert.ToDecimal如果给定空字符串,则返回 0。decimal.Parse如果要解析的字符串为空,则将抛出 ArgumentNullException。

