Exact duplicate: .Net Parse versus Convert can anyone help me?
4 Answers
Convert.ToInt32(string)
and Int32.Parse(string)
yield identical results except when the string is actually a null.
In this case, Int32.Parse(null)
throws an ArgumentNullException
But, Convert.ToInt32(null)
returns a zero.
So it is better to use Int32.Parse(string)

- 7,190
- 5
- 24
- 29
-
6Which one is better actually depends on your needs. – Joey May 02 '09 at 09:48
If you need to convert an int you have 3 options.
int.Parse
Convert.ToInt32
int.TryParse
Of the three the best solution is usually int.TryParse. The difference between these three is both speed and reliability.
int.Parse will throw an exception if any value other than an int is passed into it. General rule of thumb is that exceptions are SLOW.
Convert.ToInt32 will handle a null and return 0 however it will throw an exception for other inputs. Once again, exceptions are slow.
int.TryParse will handle almost all inputs. It will return true or
false depending on whether the passed argument was converted. If the result is false, the converted int is 0. If the result was true, the int was converted and you have a converted int.
I would not use int.Parse for anything. If I was certain that my input was going to be correct and I had some sort of form validation I would use Convert.ToInt32 just to save myself typing extra lines. In all other circumstances I would use int.TryParse
Syntax for each:
int result;
boolean success;
result = int.Parse(null) = Exception
result = Convert.ToInt32(null) = 0
success = int.TryParse(null, out result) = False/0
result = int.Parse("asd") = Exception
result = Convert.ToInt32("asd") = Exception
success = int.TryParse("asd", out result) = False/0

- 564
- 7
- 22
The parse method gives you more options for numeric formats. Other than that, they're virtually identical.
MSDN says:

- 2,802
- 19
- 18
Int.Parse() try to parse can also accept format
Int.Parse(String, NumberStyles)
you can also specify out parameter and parse will just return true or false to show whether parsing was successful or not

- 6,654
- 4
- 34
- 44