[ACCEPTED]-what is the difference between Convert.ToInt16 or 32 or 64 and Int.Parse?-c#

Accepted answer
Score: 10

Convert.ToInt converts an object to an integer and returns 3 0 if the value was null.

int x = Convert.ToInt32("43"); // x = 43;
int x = Convert.ToInt32(null); // x = 0;
int x = Convert.ToInt32("abc"); // throws FormatException

Parse convert a string to an 2 integer and throws an exception if the value 1 wasn't able to convert

int x = int.Parse("43"); // x = 43;
int x = int.Parse(null); // x throws an ArgumentNullException
int x = int.Parse("abc"); // throws an FormatException
Score: 1

Convert.ToInt32 will return 0 if the input string is null. Int32.Parse will 1 throw an exception.

Score: 1
  1. Convert.To(s) doesn't throw an exception when argument 5 is null, but Parse() does.Convert.To(s) returns 0 when argument 4 is null.

  2. Int.Parse() and Int.TryParse() can only convert strings. Convert.To(s) can 3 take any class that implements IConvertible.Hence, Convert.To(s) is 2 probably a wee bit slower than Int.Parse() because it has to ask 1 its argument what it's type is.

More Related questions