接线员'??' 不能应用于'System.DateTime'类型的操作数

Any*_*are 7 c# asp.net collections foreach operators

我收到以下错误:

Operator '??' cannot be applied to operands of type 'System.DateTime'
Run Code Online (Sandbox Code Playgroud)
 foreach (EndServReward r in reward)
                            {
                                if (con.State == ConnectionState.Closed)
                                {
                                    con.Open();
                                }
                                myIfxCmd.Parameters[0].Value = r.EmpNum ;
                                myIfxCmd.Parameters[1].Value = (r.ServDate) ?? DBNull.Value;
                            }
Run Code Online (Sandbox Code Playgroud)

在哪里reward is List<EndServReward> reward,为什么会这样,以及如何解决它?

SLa*_*aks 14

??是空合并运算符.
将它应用于不可能的值是没有意义的null.

  • @just_name这就是为什么它不能为null:http://msdn.microsoft.com/en-us/library/vstudio/1t3y8s4s.aspx (2认同)

Wim*_*ets 7

默认情况下,nullgalescing运算符不能应用于本质上不可为空的类型DateTime.无论如何你想要使用它,你必须预见到DateTime使用例如可以作为可空类型DateTime? dt;


Ado*_*rez 5

您的r.ServDate属性需要可为空:

public DateTime? ServDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

默认情况下,DateTime不能为空