0

Based on reading Nullable Types, I understand that it's proper to use Nullable for a primitive type that may be null in a database.

Is it necessary to use the ? (Nullable) type for objects?

Example:

public DateTime? DateCreated {get; set; }

or

 public DateTime DateCreated {get; set; }
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • 1
    Correction: it's proper to use `Nullable` for a value type that may be null in the database. That includes, among others, `DateTime` (and `DateTimeOffset`). – Patryk Ćwiek Jul 03 '13 at 16:08

2 Answers2

1

Your example is correct. You will need DateTime? to have the ORM (say NH) treat it as a nullable type. For objects you dont need to put the question mark.

Also, on a side-note, the question mark T? is an alias for Nullable<T>. The compiler will translate the T? to Nullable<T>. You can check this on the debugger if you want a proof :)

Yannis
  • 6,047
  • 5
  • 43
  • 62
0

In your example, Nullable<T> is valid as DateTime is not a reference type. However, for objects (and types derived from object), you cannot use Nullable<T> as the compiler will throw an error; see: C# nullable string error

Community
  • 1
  • 1
wgraham
  • 1,383
  • 10
  • 16