Int32.Parse() VS Convert.ToInt32()?

Viewed 5834
intID1 = Int32.Parse(myValue.ToString());
intID2 = Convert.ToInt32(myValue);

Which one is better and why?

3 Answers

It depends on what you mean by "better" because "better" is subjective.

For instance - code readability. Some people prefer to see "Convert" in their code; others prefer to see "Parse".

In terms of speed, they're also both roughly equal according to these benchmarks.

Or do you always wants a value returned? As others have mentioned, ConvertTo returns a 0 (zero) for null values whereas you don't get that option with Parse.

Related