什么"日期时间&QUOT?;意味着在C#?日期、时间、QUOT

2023-09-02 20:41:51 作者:不回头

我在读一本书的.Net,而在的code例子之一有与此字段定义一个类:

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 的对象的,不是的引用,当你声明该类型的字段或变量。而且,以同样的方式为 INT 不能,所以可以这样日期时间对象不能为空,因为它不是一个参考。

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.

添加问号把它变成一个的可空类型的,这意味着的或者的是一个的DateTime 对象的或的是

Adding the question mark turns it into a nullable type, which means that either it is a DateTime object, or it is null.

的DateTime 是语法糖可空< D​​ateTime的> ,其中的 可空 本身就是一个结构

DateTime? is syntactic sugar for Nullable<DateTime>, where Nullable is itself a struct.