Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable DateTime?

Tags:

c#

nullable

how to create setter and getter Properties for nullable datetime. 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?

private DateTime? mTimeStamp;

public DateTime TimeStamp
{

}
like image 539
hazem Avatar asked Jan 12 '12 17:01

hazem


2 Answers

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. They need to match, of course, and auto-properties handles that for you nicely.

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.

like image 157
Yuck Avatar answered Nov 16 '22 03:11

Yuck


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

public DateTime? TimeStamp { get; set; }
like image 24
John Kalberer Avatar answered Nov 16 '22 03:11

John Kalberer