views:

3051

answers:

7

I was browsing Scott Hanselman's Developer Interview question list, and ran across this question:

What is wrong with DateTime.Parse(myString)?

While I know there are inherent risks in parsing a string of unknow format or origin, are there other reasons? Is it to use DateTime.ParseExact instead? Should it be myString.ToString() first?

+14  A: 

In addition the locale problem, DateTime.Parse() could also throw an exception which you would then have to catch. Use DateTime.TryParse() or DateTime.TryParseExact() instead.

Joel Coehoorn
TryParse/TryParseExact are great for when the data has come from an unreliable source. If you have good reason to believe that invalid data represents a significant system error, however, throwing an exception is the right thing to do. It's not a good idea to universally change Parse into TryParse.
Jon Skeet
wow joel, you are quick and accurate as always!
Mike Brown
Well, mostly accurate-- Jon Skeet makes a good point. I stand by my response though: I think there are a lot more cases where you want the TryParse() behavior.
Joel Coehoorn
If you think the format may be incorrect, that isn't really exceptional is it?
Mr Shoubs
A: 

this blog post explains it, but the general thing is that there's no cultureinfo associated with the parse.

Stephen Wrighton
+7  A: 

Using the current thread culture on the system is often a bad idea, as is "try a variety of formats, and see if any of them work."

ParseExact with a specific culture is a much more controlled and precise approach. (Even if you specify the current culture, it makes it more obvious to readers that that's what's going on.)

Jon Skeet
+4  A: 

As MSDN Puts it:

Because the Parse(String) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results. If a specific date and time format will be parsed across different locales, use the DateTime.Parse(String, IFormatProvider) method or one of the overloads of the ParseExact method and provide a format specifier.

Torbjörn Gyllebring
+1  A: 

In addition to unknown user input the environment may be unknown.., so I guess that even if you control the input format, what the parse expect may be different..

Torbjørn
+3  A: 

That question is just to see if the developer knows the issues with that. First you should use TryParse because Parse throws an exception if it's unparseable. Also it doesn't take locale into account so in a web scenario, if a british User types 02/10/2008, and my server is using an en-US locale, I get February 10,2008 instead of October 2, 2008.

There might be other issues but those are the first two that sprung to mind.

Mike Brown
+1  A: 

My gut reaction is going to be that you hit it with unknown formats/origins. There may be other reasons -- for example, from that single line, do we know that myString is a string? (I'm assuming it is, of course.)

Generally I recommend the TryParse method instead. It's slightly more verbose, but helps prevent exceptions -- as long as your code behaves appropriately in the case of invalid input.

Of course, based on your wording to this ... I assume you already knew all that. :)

John Rudy