Better use int.Parse or Convert.ToInt32

I cannot answer based on performance, but my preferred method is always int.tryparse(mystring, out myint) as that gives a clean failure that you can test for in the program flow (rather than doing a try/catch).


Convert.ToInt32 is for dealing with any object that implements IConvertible and can be converted to an int. Also, Convert.ToInt32 returns 0 for null, while int.Parse throws a ArgumentNullException.

int.Parse is specifically for dealing with strings.

As it turns out, the string type's IConvertible implementation merely uses int.Parse in its ToInt32 method.

So effectively, if you call Convert.ToIn32 on a string, you are calling int.Parse, just with slightly more overhead (a couple more method calls).

This is true for any conversion from string to some primitive type (they all call Parse). So if you're dealing with strongly-typed string objects (e.g., you're parsing a text file), I'd recommend Parse, simply because it's more direct.

Converting arbitrary objects (returned to you by some external library, for instance) is the scenario where I'd opt for using the Convert class.


There's not much difference. Here's a quote found on msdn.

Basically the Convert class makes it easier to convert between all the base types.

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. MSDN