C# ?: Conditional Operator

后端 未结 8 1764
醉梦人生
醉梦人生 2020-12-05 06:26

I have this extract of C# 2.0 source code:

object valueFromDatabase;
decimal result;
valueFromDatabase = DBNull.Value;

result = (decimal)(valueFromDatabase          


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-05 07:13

    Your line should be:

    result = valueFromDatabase != DBNull.value ? (decimal)valueFromDatabase : 0m;
    

    0m is the decimal constant for zero

    Both parts of a conditional operator should evaluate to the same data type

提交回复
热议问题