I got an interesting email from a customer today, asking for my opinion on how to deal with the concept of “Empty” in relation to DateTime values. They had decided to use the DateTime.MinValue value as an indication that the DateTime was empty.

The two options they were consider were:

1) Call static functions to determine whether a DateTime is empty
2) Just compare the DateTime value to DateTime.MinValue to see if it is empty.

After a bit of reflection, I decided that I didn't like either approach. The problem is that they're trying to add a new concept of “emptiness” (some might equate this to “null”) to a type without changing the type.

A better approach is to define a new type that supports the concept of emptiness. In this case, we'll create a struct that encapsulates the DateTime value and lets us deal with empty in a more robust manner.

Here's the struct I wrote for them (and, no, this is not an indication that I'm now writing classes when people ask me to). 

public struct EmptyDateTime
    DateTime dateTime;
public EmptyDateTime(DateTime dateTime) { this.dateTime = dateTime; }
public bool IsEmpty { get { return dateTime == DateTime.MinValue; } }
public static explicit operator DateTime(EmptyDateTime emptyDateTime) { if (emptyDateTime.IsEmpty) throw new InvalidOperationException("DateTime is Empty"); return emptyDateTime.dateTime; }
public static implicit operator EmptyDateTime(DateTime dateTime) { return new EmptyDateTime(dateTime); }
public static EmptyDateTime Empty { get { return new EmptyDateTime(DateTime.MinValue); } } }