Why is null not allowed for DateTime in C#?

前端 未结 6 776
情书的邮戳
情书的邮戳 2021-01-01 08:42

Why it is not allowed to assign null to a DateTime in C#? How has this been implemented? And can this feature be used to make your own classes non-nullable?

Example:

6条回答
  •  误落风尘
    2021-01-01 09:24

    DateTime is a value-type (struct), where-as string is a reference-type (class etc). That is the key difference. A reference can always be null; a value can't (unless it uses Nullable - i.e. DateTime?), although it can be zero'd (DateTime.MinValue), which is often interpreted as the same thing as null (esp. in 1.1).

提交回复
热议问题