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 oledbconnection c#?

0 Answers  


What is the difference between virtual and override in c#?

0 Answers  


If casting fails what type of exception is thrown?

0 Answers  


What is the advantage of singleton class?

0 Answers  


What is the size of a decimal?

0 Answers  






Is everything an object c#?

0 Answers  


What is thread and explain states of a thread in c#?

0 Answers  


What is meant by desktop application?

0 Answers  


When you inherit a protected class-level variable, who is it available to?

2 Answers  


Why do we use struct in c#?

0 Answers  


What is exe in c#?

0 Answers  


What is the name of the implicit input parameter of a set accessor for any property?

1 Answers   TCS,


Categories