1

Possible Duplicate:
Which is preferred: Nullable<>.HasValue or Nullable<> == null?

I have field DateTime? Date

I need check Date for null and set min value.

var anyDate = Date ?? DateTime.MinValue();

or

var anyDate = Date.HasValue ? Date.Value : DateTime.MinValue();

what is right ?

Community
  • 1
  • 1
Mediator
  • 14,951
  • 35
  • 113
  • 191

2 Answers2

3

Both are. First one is shorter and then has my preference. Check : Which is preferred: Nullable<>.HasValue or Nullable<> != null?

Community
  • 1
  • 1
Kek
  • 3,145
  • 2
  • 20
  • 26
1

I think you can use this

var anyDate = Date.GetValueOrDefault(DateTime.MinValue);

Since you want to check for null and in that case assign a default value, this roughly translates to the form

if(Date.HasValue)
   return Date.Value;
return defaultvalue;
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82