“DateTime"是什么?在 C# 中是什么意思? [英] What does "DateTime?" mean in C#?
问题描述
我正在阅读一本 .NET 书籍,在其中一个代码示例中有一个包含此字段的类定义:
I am reading a .NET book, and in one of the code examples there is a class definition with this field:
private DateTime? startdate
DateTime?
是什么意思?
推荐答案
由于 DateTime
是 struct
,而不是 class
,你得到当您声明该类型的字段或变量时,是 DateTime
object,而不是 reference.
Since DateTime
is a struct
, not a class
, you get a DateTime
object, not a reference, when you declare a field or variable of that type.
并且,与 int
不能为 null
一样,所以这个 DateTime
对象永远不能为 null代码>,因为它不是参考.
And, in the same way as an int
cannot be null
, so this DateTime
object can never be null
, because it's not a reference.
添加问号会将其变成 可空类型,这意味着要么它是一个DateTime
对象,或者它是null
.
Adding the question mark turns it into a nullable type, which means that either it is a DateTime
object, or it is null
.
DateTime?
是 Nullable
的语法糖,其中 Nullable
本身就是一个 struct
.
DateTime?
is syntactic sugar for Nullable<DateTime>
, where Nullable
is itself a struct
.
这篇关于“DateTime"是什么?在 C# 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!