Why can't I assign null to decimal with ternary operator?

后端 未结 6 845
故里飘歌
故里飘歌 2021-01-17 08:03

I can\'t understand why this won\'t work

decimal? compRetAmount = !string.IsNullOrEmpty(txtLineCompRetAmt.Text) 
    ? decimal.Parse(txtLineCompRetAmt.Text.R         


        
6条回答
  •  清酒与你
    2021-01-17 08:40

    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?

提交回复
热议问题