System.DateTime的? VS的System.DateTime [英] System.DateTime? vs System.DateTime
问题描述
我在写一些code,我需要阅读我的网页从Calendar控件的日期值(Ajax工具包:日历扩展)。
I was writing to some code where I needed to read the date value from a Calendar control in my page (Ajax toolkit: calendar extender).
的code如下:
DateTime newSelectedDate = myCalendarExtender.SelectedDate;
提供了以下错误:
Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
然而,通过插入投我可以得到code的工作:
However, by inserting a cast I can get the code to work:
DateTime newSelectedDate = (DateTime)myCalendarExtender.SelectedDate; // works fine!
在'SelectedDate属性日历控件(Ajax工具包)描述的数据类型为System.DateTime的? ......显然是?有事做这一切。
The 'SelectedDate' property for the calendar control (Ajax toolkit) describes the data type as 'System.DateTime?' ... clearly the '?' has something to do with all of this.
到底是什么时候发生的一个数据类型包含这个符号(?)...... I $,我可以适用SelectedDate'属性直进式日期时间的变量,而无需进行转换p $ psumed。
What exactly is happening when a data type contains this symbol (?)... I presumed that I could apply the 'SelectedDate' property straight into a variable of type 'DateTime' without casting.
感谢
推荐答案
?即开型为空。有关详细信息,请参阅例如 MSDN
? means that the type is nullable. For details, see e.g. MSDN
可为空值大约是值类型编译器支持的包装,使值类型变成空。
Nullable is a compiler-supported wrapper around value types that allows value types to become null.
要访问日期时间值,你需要做到以下几点:
To access the DateTime value, you need to do the following:
DateTime? dateOrNull = myCalendarExtender.SelectedDate;
if (dateOrNull != null)
{
DateTime newSelectedDate = dateOrNull.Value;
}
这篇关于System.DateTime的? VS的System.DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!