Thursday, January 25, 2007
« LINQ | Main | Netgear SPH200D (Skype) »
int? i;
object o;

// this is ok:
if (i.HasValue)
    o = i;
else
    o = DBNull.Value;

// This fails to compile. Error 1 Type of conditional expression cannot be determined
// because there is no implicit conversion between 'int?' and 'System.DBNull'
o = i.HasValue ? i : DBNull.Value;

// these also fail for the same reason
o = i == null ? DBNull.Value : i;
o = i.HasValue ? i : "ABC";

// but these works:
o = i.HasValue ? (object)i : DBNull.Value;
o = i.HasValue ? (object)i : (object)DBNull.Value;
o = i.HasValue ? i : (object)DBNull.Value;
kick it on DotNetKicks.com   Thursday, January 25, 2007 11:01:10 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [1]  |