[英]Nullable DateTime?
how to create setter and getter Properties for nullable datetime. 如何为可为空的日期时间创建setter和getter属性。 for example: 例如:
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或者我是否将其声明为public?
private DateTime? mTimeStamp;
public DateTime TimeStamp
{
}
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 private
member variable or the property's data type. 如果您遇到编译器问题,可能是因为您只更改了其中一个关联部分 - private
成员变量或属性的数据类型。 They need to match, of course, and auto-properties handles that for you nicely. 当然,他们需要匹配和自动属性处理对你很好。
EDIT Just to further clarify, DateTime?
编辑只是为了进一步澄清, DateTime?
is not merely decorated with an ?
不仅仅装饰着?
attribute - it's entirely different from DateTime
. 属性 - 它与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
. 是Nullable<DateTime>
简写,它是一个泛型( Nullable<T>
),它通过包装泛型参数T
(一个struct
为非引用类型提供可为空的支持。
您可以使用与普通DateTime
属性相同的方式创建属性:
public DateTime? TimeStamp { get; set; }
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
A nullable DateTime is a discrete type from a regular DateTime and can be used like any other type. 可以为空的DateTime是常规DateTime中的离散类型,可以像任何其他类型一样使用。 So your code would be: 所以你的代码是:
private DateTime? mTimeStamp;
public DateTime? TimeStamp
{
get { return mTimeStamp; }
set { mTimeStamp = value; }
}
It's the same as non-nullable: 它与非可空的相同:
public DateTime? TimeStamp { get; set; }
You can replace DateTime
with DateTime?
你可以用DateTime
替换DateTime?
in your top sample code (looks like code is missing at the bottom of your post). 在您的顶部示例代码中(看起来您的帖子底部缺少代码)。
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;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.