在我的班上,我有这些二传手/吸气剂:
public int Id { get; set; }
public String ProjectName { get; set; }
public String ProjectType { get; set; }
public String Description { get; set; }
public String Status { get; set; }
public DateTime StartDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
DateTime是一种不可为空的类型.因此,当我从遗留数据库中检索我传递给类构造函数的数据时,当StartDate为null时出现错误.
我该如何设计呢?
谢谢Eric
Dan*_*ite 14
您可以从.NET 2.0开始使任何结构可为空.
public DateTime? StartDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
请注意?.它是一个编译器操作员Nullable<DateTime>.
将它从读卡器中拉出时,您可以执行此操作
obj.StartDate = reader["StartDate"] as DateTime?;
Run Code Online (Sandbox Code Playgroud)
以下是有关可空类型的更多信息:http://www.codeproject.com/Articles/275471/Nullable-Types-in-Charp-Net