问题描述
如何创建setter和getter属性为空的日期时间。
例如:
how to create setter and getter Properties for nullable datetime.for example:
private DateTime mTimeStamp;
public DateTime TimeStamp
{
get { return mTimeStamp; }
set { mTimeStamp = value; }
}
是否为空的属性支持setter和getter还是我声明它公开?
Does nullable attributes support setter and getter or have i to declare it public?
private DateTime? mTimeStamp;
public DateTime TimeStamp
{
}
推荐答案
您可以只是做这个:
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. They need to match, of course, and auto-properties handles that for you nicely.
修改只是为了进一步明确,<$ C $ ?C>日期时间不仅仅是装饰着
属性 - 它的完全的从日期时间。 的DateTime 简写为可空< DateTime的>
,这是一个通用(的)通过包装非引用类型为空的提供支持泛型参数 T
,这是一个结构
。
EDIT Just to further clarify, DateTime?
is not merely decorated with an ?
attribute - it's entirely different 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!