什么"日期时间&QUOT?;意味着在C#? [英] What does "DateTime?" mean in C#?

查看:259
本文介绍了什么"日期时间&QUOT?;意味着在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读一本书的.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.

这篇关于什么&QUOT;日期时间&QUOT?;意味着在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆