简体   繁体   English

System.DateTime? vs System.DateTime

[英]System.DateTime? vs System.DateTime

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). 我正在写一些代码,需要从页面中的Calendar控件读取日期值(Ajax工具包:Calendar Extender)。

The code below: 下面的代码:

DateTime newSelectedDate = myCalendarExtender.SelectedDate;

gives the following error: 给出以下错误:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

However, by inserting a cast I can get the code to work: 但是,通过插入强制转换,我可以使代码正常工作:

DateTime newSelectedDate = (DateTime)myCalendarExtender.SelectedDate; // works fine!

The 'SelectedDate' property for the calendar control (Ajax toolkit) describes the data type as 'System.DateTime?' 日历控件(Ajax工具箱)的“ SelectedDate”属性将数据类型描述为“ System.DateTime?”。 ... clearly the '?' ...显然是“?” has something to do with all of this. 与这一切有关。

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. 当数据类型包含此符号(?)时,到底发生了什么?我想我可以将'SelectedDate'属性直接应用到'DateTime'类型的变量中,而无需进行转换。

Thanks 谢谢

? means that the type is nullable. 表示类型可以为空。 For details, see eg MSDN 有关详细信息,请参见例如MSDN。

Nullable is a compiler-supported wrapper around value types that allows value types to become null. Nullable是值类型的编译器支持的包装器,该包装器允许值类型变为null。

To access the DateTime value, you need to do the following: 要访问DateTime值,您需要执行以下操作:

DateTime? dateOrNull = myCalendarExtender.SelectedDate;
if (dateOrNull != null)
{
    DateTime newSelectedDate = dateOrNull.Value;
}

DateTime? is the same as Nullable<DateTime> That is: an instance of DateTime? Nullable<DateTime>相同,即: DateTime?的实例DateTime? can contain 'NULL', whereas an instance of DateTime does not. 可以包含'NULL',而DateTime的实例不包含。 (This is true for all value - types since .NET 2.0. A Value type cannot contain NULL , but, as from .NET 2.0, nullable value types are supported via the Nullable<T> construct (or the ? shorthand). (从.NET 2.0开始的所有值类型都是如此。值类型不能包含NULL,但从.NET 2.0开始,可空值类型通过Nullable<T>构造(或?简写)得到支持。

You can get the value of DateTime? 您可以获取DateTime的值吗? and put it in DateTime by doing this: 并通过以下操作将其放在DateTime中:

DateTime? someNullableDate;
DateTime myDate;

if( someNullableDate.HasValue )
   myDate = someNullableDate.Value;

Another, conciser way to get the value of a Nullable, is by using the null-coalescing operator: 另一种简洁的方法来获取Nullable的值,是通过使用null-coalescing运算符:

DateTime myDate = someNullableDate?? default(DateTime);

具有nullable类的更好的解决方案和IMO的所有优点

DateTime newSelectedDate = myCalendarExtender.SelectedDate.GetValueOrDefault();

The Calendar control returns a Nullable<DateTime> (shorthand in C# is DateTime?) in its SelectedDate Property, since DateTime is a struct. 由于DateTime是一个结构,因此Calendar控件在其SelectedDate属性中返回Nullable <DateTime>(C#中的简写为DateTime?)。 The null value allows the Control to have a "no date selected" state. 空值使控件具有“未选择日期”状态。 Thus, you'll need to check if the nullable has a value before you can use it. 因此,您需要先检查nullable是否具有值,然后才能使用它。

var nullable = myCalendarExtender.SelectedDate;
var newSelectedDate = (nullable.HasValue) ? nullable.Value : SomeDefaultValue;

EDIT: Even more concise, thanks to Josh's comment: 编辑:更加简洁,感谢乔希的评论:

var newSelectedDate = myCalendarExtender.SelectedDate ?? SomeDefaultValue;

I like it! 我喜欢!

You can use the ?? 您可以使用?? operator to work with nullables in a very concise way in C#. 运算符,以便在C#中以非常简洁的方式处理nullable。 See my comment on Travis's answer for a shorter way of expressing the "if not null then use it else use some default value" concept. 请参阅我对Travis答案的评论,以获得表达“如果不为null则使用它,否则使用一些默认值”概念的更短方法。 They both do the same thing. 他们俩都做同样的事情。

var Endtime = DateTime.Now();
DateTime startTime = item.REPORT_TIME.HasValue ? item.REPORT_TIME.Value : Endtime;

Means: the type of item.item.REPORT_TIME is system.DateTime? 意思是:item.item.REPORT_TIME的类型是system.DateTime?
howerver the type of startTime is system.DateTime; 但是startTime的类型是system.DateTime; so the code can changed it like 所以代码可以像

`var Endtime=DateTime.Now; var Endtime = DateTime.Now; var startTime=item.REPORT_TIME.HasValue?Convert.ToDateTime(item.REPORT_TIME.HasValue):Endtime var startTime = item.REPORT_TIME.HasValue?Convert.ToDateTime(item.REPORT_TIME.HasValue):Endtime

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM