我今天注意到,如果你声明一个可以为空的DateTime对象,你就不会获得与拥有标准DateTime对象时相同的函数集.
例如:
DateTime? nullableDT = DateTime.Now;
DateTime regularDT = DateTime.Now;
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,nullableDT不能使用以下任何功能:
ToFileTime
ToFileTimeUtc
ToLocalTime
ToLongDateString
ToLongTimeString
ToOADate
ToShortDateString
ToShortTimeString
ToString(IFormatProvider)
ToString(String)
ToString(String, IFormatProvider)
ToUniversalTime
Run Code Online (Sandbox Code Playgroud)
这是短名单,还有更多方法不可用.
为什么.NET表现得像这样?我绕过可以Convert.ToDateTime()
约会的日期来解决这个问题,但这看起来很克服......有更好的方法吗?
That's because you need to call Nullable<T>.Value
to get the actual DateTime
value.