What is the difference between Int.parse and Convert.ToInt32

Both the int.Parse and Convert.ToInt32 will return the same result. But the null handling only difference between them.

For example,
string str = "15";
int Val1= Convert.ToInt32(str);
int Val2= int.Parse(str);

From the above code, both Val1 and Val2 return the value as 15. Suppose the string value str has null value, Convert.ToInt32 will return zero and int.Parse will throw ArgumentNullException error.

For example,
string str=null;
int Val1= Convert.ToInt32(str);
int Val2 = int.Parse(str); //This will throw ArgumentNullException Error

That's it...

S.VinothkumaR.

5 comments:

chintu reddy said...

Hi Vinoth,

how are you ?
Your blog is really usefull.

Unknown said...

Nice & useful Dude....

Unknown said...

Nice & useful Dude....

MahendraVarma.K said...

Its really useful Vinoth. Thank you.

MahendraVarma.K said...

Its really useful Vinoth. Thank you.