C# 使用单行“if”将“null”值分配给 Nullable<DateTime>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12855018/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:42:03  来源:igfitidea点击:

Assigning `null` value to Nullable<DateTime> with single line 'if'

c#datetimenull

提问by Nalaka526

I have a Class like this

我有一个这样的班级

public class MyClass
{
    public int Id { get; set; }
    public Nullable<DateTime> ApplicationDate { get; set; }
    ....
}

Now I'm trying to fill an object of MyClasslike this

现在我试图填补对象的MyClass这样

DataTable dt = DBHelper.GetDataTable(sql, conn);
DataRow dr = dt.Rows[0];

MyClass oMyClass = new MyClass();
oMyClass.Id = (int)dr["Id"];
oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value ? null : Convert.ToDateTime(dr["AppDate"]); 
//Above line gives an error
....

Assigning of Application Date value gives an error

分配应用程序日期值会出错

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'

What am I missing here?

我在这里缺少什么?

采纳答案by Jon

You need to cast nullto DateTime?:

你需要投射nullDateTime?

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value 
    ? (DateTime?)null 
    : Convert.ToDateTime(dr["AppDate"]); 

This is because of the way the compiler determines the resulting type of the conditional operator; the behavior is by design:

这是因为编译器确定条件运算符的结果类型的方式;该行为是设计使然:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

first_expression 和 second_expression 的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换。

Since nullby itself is of null typeand thus there is no conversion from or to it, you need to help the compiler by casting.

由于null它本身是空类型,因此没有从它到它的转换,你需要通过强制转换来帮助编译器。

回答by George Duckett

oMyClass.ApplicationDate =
    dr["ApplDate"] == DBNull.Value ?
    (DateTime?)null :
    Convert.ToDateTime(dr["AppDate"]);

All the compiler knows is that one thing evaluates to a nulland the other evaluates to a DateTime. The compiler complains because it can't convert from one to the other so it's up to you to cast them to something that can be both values.

编译器只知道一件事的计算结果为 a null,而另一项的计算结果为 a DateTime。编译器抱怨是因为它无法从一个转换为另一个,所以你可以将它们转换为可以是两个值的东西。

Note that DateTime?is short for Nullable<DateTime>.
Also note that you only need to cast the null value as there is an implicit conversion between DateTime?and DateTimeso the compiler can do that conversion its self.

请注意,它DateTime?是 的缩写Nullable<DateTime>
另请注意,您只需要强制转换 null 值,因为两者之间存在隐式转换DateTime?DateTime因此编译器可以自行执行该转换。

回答by Botz3000

try this:

尝试这个:

oMyClass.ApplicationDate = 
    dr["ApplDate"] == DBNull.Value ? (DateTime?)null : 
                                     Convert.ToDateTime(dr["AppDate"]); 

You can also apply the cast to the last expression.

您还可以将强制转换应用于最后一个表达式。

回答by KaR

You will need to convert "null" into Nullable

您需要将“null”转换为 Nullable

Try this code:

试试这个代码:

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dr["AppDate"]);

回答by Hardik

DateTime? dt = (DateTime?)null;

or

或者

Nullable<DateTime> dt = (Nullable<DateTime>)null;

回答by civilator

You can use defaultwhich will assign the default value of an uninitialized Type.

您可以使用defaultwhich 将分配未初始化类型的默认值。

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value ? default(Nullable<DateTime>) : Convert.ToDateTime(dr["AppDate"]); 

More examples

更多例子

bool isHappy = default(bool); //isHappy = false
int number = default(int); //number = zero
string text = default(text); // text = null
MyObject myObject = default(MyObject); // myObject = null
DateTime? date = default(DateTime?); //date = null