3

My C# class has a date/time property that I will use Code First to map to a column in an SQL Server database. This date will never be prior or close to default( DateTime ). If I type the property as System.DateTime then I could always check for an unset value with MyProp == default( DateTime ).

Is there a compelling reason whether or not to type this property as a (nullable) DateTime? instead of a (non-nullable) DateTime? Why?

4

1 回答 1

1

A sentinel value for some type like an integer in my opinion make sense when it relates to a record ID. It makes less sense when zero is a legitimate at value (as in the value could also be negative). Dates always have meaning so null is more informative as a "we don't when" value. Also I think it makes a "give me all records where we don't have a date" query look much cleaner.

于 2013-10-30T21:12:52.103 回答