“日期时间”是什么?在 C# 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/109859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "DateTime?" mean in C#?
提问by Alvin S
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?
意思?
采纳答案by Thomas
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.
由于DateTime
is a struct
,而不是 a ,因此当您声明该类型的字段或变量时class
,您将获得一个DateTime
object,而不是一个reference。
And, in the same way as an int
cannot be null
, so this DateTime
object can never be null
, because it's not a reference.
并且,与int
不能是一样null
,所以这个DateTime
对象永远不可能是null
,因为它不是引用。
Adding the question mark turns it into a nullable type, which means that eitherit is a DateTime
object, orit is null
.
添加问号将其变为可空类型,这意味着它要么是DateTime
对象,要么是null
。
DateTime?
is syntactic sugar for Nullable<DateTime>
, where Nullable
is itself a struct
.
DateTime?
是 for 的语法糖Nullable<DateTime>
,哪里Nullable
是struct
.
回答by Daniel Auger
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 值类型表示日期和时间,其值范围从 0001 年 1 月 1 日午夜 12:00:00 Anno Domini 或 AD(也称为 Common Era,或 CE)到 11:59:59 PM,9999 年 12 月 31 日广告 (CE)
DateTime?
can be null however.
DateTime?
但是可以为空。
回答by Ted Elliott
It's equivalent to Nullable< DateTime>. You can append "?" to any primitive type or struct.
它相当于 Nullable<DateTime>。您可以附加“?” 到任何原始类型或结构。
回答by Jorge Ferreira
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>.
约会时间?在语法上等同于Nullable<DateTime>。
回答by mattlant
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.
它基本上为您提供了一个额外的基元状态。它可以是一个值,也可以为空。它在不需要分配值的情况下很有用。因此,而不是使用例如 datetime.min 或 max,您可以将其分配为 null 来表示没有值。
回答by Sonu Rajpoot
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 是一个结构意味着 DateTime 是一个值类型,所以你得到一个 DateTime 对象,而不是一个引用,因为 DateTime 不是一个类,当你声明一个该类型的字段或变量时,你不能用 null 初始化,因为值类型不接受空值。就像 int 不能为 null 一样。所以 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;
约会时间?日期 = 空;
int? intvalue = null;
内部?整数值 = 空;
In above code, variable dateis an object of DateTime or it is null. Same for intvalue.
在上面的代码中,变量date是 DateTime 的对象或者它为 null。与整数值相同。
回答by Parikshit
public class ReportsMapper : CommonMapper
{
public DateTime? cb_Bill_From_Date { get; set; }
public DateTime? cb_Bill_To_Date { get; set; }
public DateTime? tff_Bill_From_Date { get; set; }
public DateTime? tff_Bill_To_Date { get; set; }
}
If you declare DateTime As Null In Procedure Then You get an error stating DateTime Object Can never be Null so you need to add ? After DateTime that will say DateTime is Nullable too.
如果您在过程中将 DateTime 声明为 Null 那么您会收到一个错误,指出 DateTime Object Can never be Null 所以您需要添加 ? 在 DateTime 之后,它也会说 DateTime 也是 Nullable。
Hope This Help!
希望这有帮助!