简体   繁体   中英

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

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 MyClass like this

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'

What am I missing here?

You need to cast null to DateTime? :

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.

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

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

All the compiler knows is that one thing evaluates to a null and 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.

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 DateTime so the compiler can do that conversion its self.

try this:

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

You can also apply the cast to the last expression.

You will need to convert "null" into Nullable

Try this code:

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

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

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
DateTime? dt = (DateTime?)null;

要么

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM