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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / jyoti magdum

Both(Convert.ToInt32 and int.Parse) will return the same
result in
most of the cases.

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 throw ArgumentNullException error.

and the int.Parse will throw ArgumentNullException error.

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

Is This Answer Correct ?    2 Yes 5 No

Post New Answer

More C Sharp Interview Questions

What is HashTable, what is the use of it, when do we use (purpose).

2 Answers  


Define sealed classes in c#?

0 Answers  


How long has c# been around?

0 Answers  


What is datatable in c#?

0 Answers  


What namespaces are necessary to create a localized application?

2 Answers  






What are generics in c#.net?

0 Answers  


What are the namespace level elements?

0 Answers  


How can an inner class access the members of outer class?

0 Answers   Changepond,


What is the base class of all classes in c#?

0 Answers  


What is difference between Convert.ToString(variable) and variable.ToString()

4 Answers   Mascon,


What are the two keywords used to pass a variable by reference in c#?

2 Answers   IBM,


can you create a function in c# which can accept varying number of arguments

0 Answers   Cognizant,


Categories