开发者

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).

The code below:

DateTime newSelectedDate = myCalendarExtender.SelectedDate;
开发者_StackOverflow中文版

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?' ... 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.

Thanks


? 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;
}


DateTime? is the same as Nullable<DateTime> That is: an instance of DateTime? can contain 'NULL', whereas an instance of DateTime does not. (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).

You can get the value of DateTime? and put it in DateTime by doing this:

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:

DateTime myDate = someNullableDate?? default(DateTime);


A much better solution and IMO the best feature of all with the nullable class

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. 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.

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#. 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. 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?
howerver the type of startTime is system.DateTime; so the code can changed it like

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

`

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜