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

Which is the best way for keeping the data in XML or SQL server..and why?

8 Answers   Infosys, Satyam,


is it possible to inherit a class but methods declared in the class should not be inheritable i possible how?

8 Answers   Microsoft,


What is a Jagged Array in C#?

0 Answers   Arigo Infotech,


What is dll in c#?

0 Answers  


Explain the difference between the debug class and trace class?

0 Answers  


What is covariance and contravariance? Did Delegate and method overriding support these?

1 Answers   TCS,


If you define a user defined data type by using the class keyword, is it a value type or reference type?

0 Answers  


Do loops c#?

0 Answers  


What are the properties of string?

0 Answers  


What is a function c#?

0 Answers  


i want display a given number into Rupees Format Like Given number is : 156735 my Expected output is 1,56,735. how to display?

6 Answers  


How can we convert XML Data to DataBase Table in C#.Net?

2 Answers   Wipro,


Categories