What is the difference between Convert.ToInt16 or 32 or 64 and Int.Parse?

Possible duplicate:
What is the main difference between int.Parse () and Convert.ToInt32

Hi

I want to know what is between them:

Convert.ToInt16 or Convert.ToInt32 or Convert.ToInt64 

against

 Int.Parse 

both of them do the same, so they just want to know what is different?

+8
c #
source share
3 answers

Convert.ToInt converts the object to an integer and returns 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 string to integer and throws an exception if the value could not be converted

 int x = int.Parse("43"); // x = 43; int x = int.Parse(null); // x throws an ArgumentNullException int x = int.Parse("abc"); // throws an FormatException 
+10
source share

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

+1
source share
  • Convert.To(s) does not throw an exception if the argument is null, but Parse() does. Convert.To(s) returns 0 when the argument is null.

  • Int.Parse() and Int.TryParse() can only convert strings. Convert.To(s) can accept any class that implements IConvertible.Hence, Convert.To(s) , wee probably have a bit slower than Int.Parse() , because it should ask its argument that it is a type.

0
source share

All Articles