Friday 6 July 2012

Can DateTimes be null?

Purpose:

  • The DateTime struct is a good example of where the value type vs reference type dynamic comes to fore. This question tests whether the developer has an understanding of the difference between the two and why DataTime behaves like it does. Indication of in depth understanding of the framework.

Potential Answer:
No it is a value type and therefore is allocated to the stack instead of the heap. And value types cannot be null. It is a struct instead of class for those interested. What one can do is wrap the DateTime value type with the Nullable reference type to get the desired behavior... Before .Net 2.0 people used to moan about this all the time...

No comments:

Post a Comment