什么“DateTime ?“在c#中意味着什么?

时间:2022-10-22 16:59:32

I am reading a .Net book, and in one of the code examples there is a class definition with this field:

我正在读一本。net的书,在其中一个代码示例中有一个类定义:

private DateTime? startdate

What does DateTime? mean?

DateTime什么?的意思吗?

6 个解决方案

#1


121  

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是一个结构体,而不是类,所以当您声明某个字段或变量时,您将得到一个DateTime对象,而不是引用。并且,就像int不能为空一样,这个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对象,要么是null。

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

DateTime吗?为Nullable 的语法糖,其中Nullable本身就是一个struct。

#2


23  

It's a nullable DateTime. ? after a primitive type/structure indicates that it is the nullable version.

这是一个可以为空DateTime。吗?在基元类型/结构之后表明它是可空版本。

DateTime is a structure that can never be null. From MSDN:

DateTime是一种永不为空的结构。从MSDN:

The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini, or A.D. (also known as Common Era, or C.E.) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.)

DateTime值类型表示日期和时间,其值从午夜12:00、1月1日到1月1日或公元(也称为Common Era,或c . e)到晚上11:59:59公元9999年12月31日

DateTime? can be null however.

DateTime吗?可以为空。

#3


13  

A ? as a suffix for a value type allows for null assignments that would be othwerwise impossible.

一个吗?作为值类型的后缀,允许不可能的空赋值。

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Represents an object whose underlying type is a value type that can also be assigned a null reference.

表示一个对象,该对象的底层类型是一个值类型,该值类型也可以分配一个空引用。

This means that you can write something like this:

这意味着你可以这样写:

    DateTime? a = null;
    if (!a.HasValue)
    {
        a = DateTime.Now;
        if (a.HasValue)
        {
            Console.WriteLine(a.Value);
        }
    }

DateTime? is syntatically equivalent to Nullable<DateTime>.

DateTime吗?在语法上等同于可空

#4


10  

It's equivalent to Nullable< DateTime>. You can append "?" to any primitive type or struct.

它等价于Nullable< DateTime>。您可以将“?”附加到任何原始类型或结构。

#5


5  

it basically gives you an extra state for primitives. It can be a value, or it can be null. It can be usefull in situations where a value does not need to be assigned. So rather than using for example, datetime.min or max, you can assign it null to represent no value.

它基本上为原语提供了一个额外的状态。它可以是一个值,也可以是null。它可以在不需要分配值的情况下使用。而不是使用datetime。最小或最大值,你可以将它赋为空来表示无值。

#6


1  

As we know, DateTime is a struct means DateTime is a value type, so you get a DateTime object, not a reference because DateTime is not a class, when you declare a field or variable of that type you cannot initial with null Because value types don't accept null. In the same way as an int cannot be null. so DateTime object never be null, because it's not a reference.

正如我们所知道的,DateTime是一个struct,意味着DateTime是一个值类型,所以你会得到一个DateTime对象,而不是一个引用,因为DateTime不是一个类,当你声明一个字段或变量时,你不能用null作为初始值,因为值类型不接受null。就像int不能为空一样。DateTime对象从不为空,因为它不是一个引用。

But sometimes we need nullable variable or field of value types, that time we use question mark to make them nullable type so they allow null.

但有时我们需要可空变量或值类型字段,这时我们使用问号使它们为空类型,这样它们就允许为空。

For Example:-

例如:-

DateTime? date = null;

DateTime吗?日期=零;

int? intvalue = null;

int ?intvalue =零;

In above code, variable date is an object of DateTime or it is null. Same for intvalue.

在上面的代码中,变量date是DateTime的对象,或者是null。intvalue相同。

#1


121  

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是一个结构体,而不是类,所以当您声明某个字段或变量时,您将得到一个DateTime对象,而不是引用。并且,就像int不能为空一样,这个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对象,要么是null。

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

DateTime吗?为Nullable 的语法糖,其中Nullable本身就是一个struct。

#2


23  

It's a nullable DateTime. ? after a primitive type/structure indicates that it is the nullable version.

这是一个可以为空DateTime。吗?在基元类型/结构之后表明它是可空版本。

DateTime is a structure that can never be null. From MSDN:

DateTime是一种永不为空的结构。从MSDN:

The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini, or A.D. (also known as Common Era, or C.E.) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.)

DateTime值类型表示日期和时间,其值从午夜12:00、1月1日到1月1日或公元(也称为Common Era,或c . e)到晚上11:59:59公元9999年12月31日

DateTime? can be null however.

DateTime吗?可以为空。

#3


13  

A ? as a suffix for a value type allows for null assignments that would be othwerwise impossible.

一个吗?作为值类型的后缀,允许不可能的空赋值。

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Represents an object whose underlying type is a value type that can also be assigned a null reference.

表示一个对象,该对象的底层类型是一个值类型,该值类型也可以分配一个空引用。

This means that you can write something like this:

这意味着你可以这样写:

    DateTime? a = null;
    if (!a.HasValue)
    {
        a = DateTime.Now;
        if (a.HasValue)
        {
            Console.WriteLine(a.Value);
        }
    }

DateTime? is syntatically equivalent to Nullable<DateTime>.

DateTime吗?在语法上等同于可空

#4


10  

It's equivalent to Nullable< DateTime>. You can append "?" to any primitive type or struct.

它等价于Nullable< DateTime>。您可以将“?”附加到任何原始类型或结构。

#5


5  

it basically gives you an extra state for primitives. It can be a value, or it can be null. It can be usefull in situations where a value does not need to be assigned. So rather than using for example, datetime.min or max, you can assign it null to represent no value.

它基本上为原语提供了一个额外的状态。它可以是一个值,也可以是null。它可以在不需要分配值的情况下使用。而不是使用datetime。最小或最大值,你可以将它赋为空来表示无值。

#6


1  

As we know, DateTime is a struct means DateTime is a value type, so you get a DateTime object, not a reference because DateTime is not a class, when you declare a field or variable of that type you cannot initial with null Because value types don't accept null. In the same way as an int cannot be null. so DateTime object never be null, because it's not a reference.

正如我们所知道的,DateTime是一个struct,意味着DateTime是一个值类型,所以你会得到一个DateTime对象,而不是一个引用,因为DateTime不是一个类,当你声明一个字段或变量时,你不能用null作为初始值,因为值类型不接受null。就像int不能为空一样。DateTime对象从不为空,因为它不是一个引用。

But sometimes we need nullable variable or field of value types, that time we use question mark to make them nullable type so they allow null.

但有时我们需要可空变量或值类型字段,这时我们使用问号使它们为空类型,这样它们就允许为空。

For Example:-

例如:-

DateTime? date = null;

DateTime吗?日期=零;

int? intvalue = null;

int ?intvalue =零;

In above code, variable date is an object of DateTime or it is null. Same for intvalue.

在上面的代码中,变量date是DateTime的对象,或者是null。intvalue相同。