What is the difference between int.Parse() and
Convert.toInt32().

Answer Posted / joseph


I got this example through net..hope it helps you.
Both(Convert.ToInt32 and int.Parse) will return the same
result in
most of the cases. But null is handled differently.
Consider the following example…

string strCount="32";
int count1 = Convert.ToInt32(strCount);
int count2 = int.Parse(strCount);

If you print the result it will be same ie 32.

If suppose the string is the null (see the example below),
Convert.ToInt32 will return zero.
but the int.Parse will throw ArgumentNullException error.

string strCount=null;
int count1 = Convert.ToInt32(strCount);
int count2 = int.Parse(strCount); // Error

Is This Answer Correct ?    50 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a class or a struct have multiple constructors?

753


Is c# strongly typed?

665


What is the use of tryparse in c#?

703


What is difference between const and static in c#?

714


What is a race condition?

691


What is the difference between finalize() and dispose()?

711


Which .gang of four. Design pattern is shown below?

700


What is the use of console readkey in c#?

714


Why do we need dependency injection in c#?

720


Can we customize the serialization process?

751


What is data hiding in c#?

699


What is class sortedlist underneath?

771


What is parsing?

733


What's the c# syntax to catch any possible exception?

700


What is the purpose of a console table?

689