解决的问题:
如何区分没有赋值的Datetime和1/1/0001
如何区分没有赋值的int 和 0
C#2.0以上的版本支持了Nullable<T>
代码:
DateTime? a = null;
Nullable<DateTime> t = null;
Console.WriteLine(a.HasValue);
解释:
DateTime? 就相当于 Nullable<DateTime>
同理可以用于其他值类型.
注意:
不能用于引用类型.
补充:
如果是使用DataReader等,从数据库读不到值的时候,可以采用 ?? 来指定一个默认值
如:
SqlDatareader dr = xxx;
int? a = dr.GetInt(0)??100