为什么我不能用三元运算符将null赋值给十进制?

时间:2021-04-13 22:30:12

I can't understand why this won't work

我无法理解为什么这不起作用

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) 
    ? decimal.Parse(txtLineCompRetAmt.Text.Replace(",","")) 
    : null;

6 个解决方案

#1


35  

Because null is 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 default keyword.

以下是默认关键字的MSDN链接。

#2


6  

Don't use decimal.Parse.

不要使用decimal.Parse。

Convert.ToDecimal will return 0 if it is given a null string. decimal.Parse will throw an ArgumentNullException if the string you want to parse is null.

如果给出一个空字符串,Convert.ToDecimal将返回0。如果要解析的字符串为null,decimal.Parse将抛出ArgumentNullException。

#3


5  

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?

问题是编译器不知道nullhas是什么类型。所以你可以把它转换为十进制?

#4


3  

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) ?  
                          decimal.Parse(txtLineCompRetAmt.Text.Replace(",","")) : 
                          (decimal?)null;

#5


3  

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 a to the type of b, or from the type of b to 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 a and b are decimal and some unknown reference or nullable type. The compiler can't guess what you mean, so you need to help it:

写条件的时候? a:b,必须有从a的类型到b的类型的隐式转换,或者从b的类型到a的类型的隐式转换。然后,编译器将推断整个表达式的类型作为此转换的目标类型。您将其分配给decimal类型的变量的事实?编译器从未考虑过。在您的情况下,a和b的类型是十进制和一些未知的引用或可空类型。编译器无法猜出你的意思,所以你需要帮助它:

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
                             ? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
                             : default(decimal?);

#6


2  

You need to cast the first part to decimal?

你需要将第一部分转换为十进制吗?

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) 
    ? (decimal?)decimal.Parse(txtLineCompRetAmt.Text.Replace(",","")) 
    : null;

#1


35  

Because null is 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 default keyword.

以下是默认关键字的MSDN链接。

#2


6  

Don't use decimal.Parse.

不要使用decimal.Parse。

Convert.ToDecimal will return 0 if it is given a null string. decimal.Parse will throw an ArgumentNullException if the string you want to parse is null.

如果给出一个空字符串,Convert.ToDecimal将返回0。如果要解析的字符串为null,decimal.Parse将抛出ArgumentNullException。

#3


5  

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?

问题是编译器不知道nullhas是什么类型。所以你可以把它转换为十进制?

#4


3  

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) ?  
                          decimal.Parse(txtLineCompRetAmt.Text.Replace(",","")) : 
                          (decimal?)null;

#5


3  

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 a to the type of b, or from the type of b to 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 a and b are decimal and some unknown reference or nullable type. The compiler can't guess what you mean, so you need to help it:

写条件的时候? a:b,必须有从a的类型到b的类型的隐式转换,或者从b的类型到a的类型的隐式转换。然后,编译器将推断整个表达式的类型作为此转换的目标类型。您将其分配给decimal类型的变量的事实?编译器从未考虑过。在您的情况下,a和b的类型是十进制和一些未知的引用或可空类型。编译器无法猜出你的意思,所以你需要帮助它:

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text)
                             ? decimal.Parse(txtLineCompRetAmt.Text.Replace(",",""))
                             : default(decimal?);

#6


2  

You need to cast the first part to decimal?

你需要将第一部分转换为十进制吗?

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) 
    ? (decimal?)decimal.Parse(txtLineCompRetAmt.Text.Replace(",","")) 
    : null;