C# System.DateTime?与 System.DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1729553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:20:15  来源:igfitidea点击:

System.DateTime? vs System.DateTime

c#asp.netdatetime

提问by Dal

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

我正在编写一些代码,我需要在我的页面(Ajax 工具包:日历扩展器)中从日历控件中读取日期值。

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?' ... clearly the '?' has something to do with all of this.

日历控件(Ajax 工具包)的“SelectedDate”属性将数据类型描述为“System.DateTime?” ...显然是“?” 与这一切都有关。

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

谢谢

采纳答案by Marek

? means that the type is nullable. For details, see e.g. 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;
}

回答by Frederik Gheysels

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

DateTime?Nullable<DateTime>即: 的实例DateTime?可以包含“NULL”,而 的实例DateTime不包含。(这适用于自 .NET 2.0 以来的所有值类型。值类型不能包含 NULL ,但是,从 .NET 2.0 开始,可以通过Nullable<T>构造(或 ? 简写)支持可空值类型。

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

你能得到DateTime的值吗?并通过执行以下操作将其放入 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 值的简洁方法是使用空合并运算符:

DateTime myDate = someNullableDate?? default(DateTime);

回答by Travis Heseman

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.

Calendar 控件在其 SelectedDate 属性中返回 Nullable<DateTime>(C# 中的简写是 DateTime?),因为 DateTime 是一个结构体。空值允许控件具有“未选择日期”状态。因此,您需要先检查 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!

我喜欢!

回答by Chris Marisic

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

一个更好的解决方案,IMO 是可空类中最好的特性

DateTime newSelectedDate = myCalendarExtender.SelectedDate.GetValueOrDefault();

回答by Josh

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.

你可以使用 ?? 运算符在 C# 中以非常简洁的方式处理可空值。请参阅我对 Travis 的回答的评论,以更短的方式表达“如果不为空,则使用它,否则使用一些默认值”概念。他们都做同样的事情。

回答by Ken yang

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

意思是:item.item.REPORT_TIME的类型是system.DateTime?
但是 startTime 的类型是 system.DateTime; 所以代码可以改变它

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

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

`

`