Thoughts on Static Code Analysis Warning CA1806 for TryParse calls

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2011-01-05T22:44:59Z Indexed on 2011/01/05 22:53 UTC
Read the original article Hit count: 349

Filed under:
|

I was wondering what people's thoughts were on the CA1806 (DoNotIgnoreMethodResults) Static Code Analysis warning when using FxCop.

I have several cases where I use Int32.TryParse to pull in internal configuration information that was saved in a file. I end up with a lot of code that looks like:

Int32.TryParse(someString, NumberStyles.Integer, CultureInfo.InvariantCulture, out intResult);

MSDN says the default result of intResult is zero if something fails, which is exactly what I want.

Unfortunately, this code will trigger CA1806 when performing static code analysis. It seems like a lot of redundant/useless code to fix the errors with something like the following:

bool success = Int32.TryParse(someString, NumberStyles.Integer, CultureInfo.InvariantCulture, out intResult);
if (!success)
{
 intResult= 0;
}

Should I suppress this message or bite the bullet and add all this redundant error checking? Or maybe someone has a better idea for handling a case like this?

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about fxcop