C# 可为空的日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8839787/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:44:56  来源:igfitidea点击:

Nullable DateTime?

c#nullable

提问by hazem

how to create setter and getter Properties for nullable datetime. for example:

如何为可为空的日期时间创建 setter 和 getter 属性。例如:

private DateTime mTimeStamp;

public DateTime TimeStamp
{
      get { return mTimeStamp; }
      set { mTimeStamp = value; }
}

Does nullable attributes support setter and getter or have i to declare it public?

可空属性是否支持 setter 和 getter 或让 i 将其声明为 public?

private DateTime? mTimeStamp;

public DateTime TimeStamp
{

}

采纳答案by Yuck

You can just do this instead:

你可以这样做:

public DateTime? TimeStamp { get; set; }

If you were having trouble with the compiler it's probably because you only changed one of the associated parts - either the privatemember variable or the property's data type. They need to match, of course, and auto-properties handles that for you nicely.

如果您在编译器方面遇到问题,那可能是因为您只更改了相关部分之一——private成员变量或属性的数据类型。当然,它们需要匹配,并且自动属性可以很好地为您处理。

EDITJust to further clarify, DateTime?is not merely decorated with an ?attribute - it's entirelydifferent from DateTime. DateTime?is shorthand for Nullable<DateTime>, which is a generic (Nullable<T>) that provides nullable support to non-reference types by wrapping the generic parameter T, which is a struct.

编辑只是为了进一步澄清,DateTime?不只是装饰用?属性-它是完全不同DateTimeDateTime?是 的简写Nullable<DateTime>,它是一个泛型 ( Nullable<T>),它通过包装泛型参数为非引用类型提供可为空的支持,泛型参数Tstruct.

回答by Abe Miessler

You should be able to make a DateTime nullable in this way:

您应该能够以这种方式使 DateTime 可以为空:

private DateTime? mTimeStamp;

public DateTime? TimeStamp
{
      get { return mTimeStamp; }
      set { mTimeStamp = value; }
}

You can use this modifier on other types as well. Read up here: http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=VS.100%29.aspx

您也可以在其他类型上使用此修饰符。在这里阅读:http: //msdn.microsoft.com/en-us/library/1t3y8s4s%28v=VS.100%29.aspx

回答by John Kalberer

You can create the property in the same way as a normal DateTimeproperty:

您可以以与普通DateTime属性相同的方式创建属性:

public DateTime? TimeStamp { get; set; }

回答by Kirk Woll

A nullable DateTime is a discrete type from a regular DateTime and can be used like any other type. So your code would be:

可为空的 DateTime 是常规 DateTime 的离散类型,可以像任何其他类型一样使用。所以你的代码是:

private DateTime? mTimeStamp;

public DateTime? TimeStamp
{
      get { return mTimeStamp; }
      set { mTimeStamp = value; }
}

回答by Cymen

It's the same as non-nullable:

它与不可为空相同:

public DateTime? TimeStamp { get; set; }

public DateTime? TimeStamp { get; set; }

You can replace DateTimewith DateTime?in your top sample code (looks like code is missing at the bottom of your post).

您可以替换DateTimeDateTime?在顶部的示例代码(貌似代码在您的文章底部缺失)。

回答by Jason Meckley

private DateTime? mTimeStamp;

public DateTime? TimeStamp
{
  get { return mTimeStamp; }
  set { mTimeStamp = value; }
}

or, if you are using .net 3.0+

或者,如果您使用的是 .net 3.0+

public DateTime? TimeStamp {get;set;}