更新时间:2022-10-15 19:39:24
由于 DateTime
是一个 code>,而不是一个
类
,你得到一个 DateTime
对象当您声明该类型的字段或变量时,引用。而且,与 int
不能 null
相同,所以这个 DateTime
object never not null,因为它不是一个引用。
添加问号将它变成一个 可空类型 ,这意味着 它是一个 DateTime
对象,或它是 null
Nullable
本身就是一个 struct
。
I am reading a .Net book, and in one of the code examples there is a class definition with this field:
private DateTime? startdate
What does DateTime?
mean?
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. And, in the same way as an int
cannot be null
, so can this DateTime
object never be null, because it's not a reference.
Adding the question mark turns it into a nullable type, which means that either it is a DateTime
object, or it is null
.
DateTime?
is syntactic sugar for Nullable<DateTime>
, where Nullable
is itself a struct
.