Friday 27 July 2012

What’s wrong with a line like this? DateTime.Parse(myString)?

Purpose:
  • This question tests either awareness of Globalization that would show experience in writing international systems or it is a nod to the difference between value and reference types.
  • Since DateTime is a value type it does not support a null type, meaning that if there is not error handling code this naive approach might have side effects.
  • An awareness of either of these issues is a good sign

Potential Answer:
There is no clear specification of what the locale is for the string. This means that one could be expecting a date in a certain format MMddyyyy but perhaps the string is ddMMyyyy. A better option would be to use ParseExact where you provide the format so that all the formats don't need to be checked to infer. Further more since DateTime is a struct or value type if something is wrong it will throw an exception.

No comments:

Post a Comment