Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between Convert.ToInt16 or 32 or 64 and Int.Parse? [duplicate]

Tags:

c#

Possible Duplicate:
Whats the main difference between int.Parse() and Convert.ToInt32

Hi

I want to know what is the different between :

Convert.ToInt16 or Convert.ToInt32 or Convert.ToInt64

vs

Int.Parse

both of them are doing the same thing so just want to know what the different?

like image 477
Saleh Avatar asked Feb 03 '23 20:02

Saleh


1 Answers

Convert.ToInt converts an 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 a string to an integer and throws an exception if the value 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
like image 113
Homam Avatar answered Feb 05 '23 10:02

Homam