what is the purpose of using int.Parse and Convert.ToInt32 method ?

 Posted by Pradeep Raturi on 2/5/2013 | Category: C# Interview questions | Views: 5021 | Points: 40

AS we know here in C sharp we have number of ways to using that we can change our string data type into integer data type, two of them are int.Parse and Convert.ToInt32.
Both are basic method and used to convert string data type into integer data type. But question arises here why we go for other method ? if we have one method like int.Parse that works same as that other method such as Convert.ToInt32 and other else works.
Reason are ,suppose if our string containing null to which we want to convert into integer while using int.Parse method, in this case we will get exception error at run time.
For the same situation where we want to convert our null string into integer using Convert.ToInt32 method, it will compile successfully without generating any exception error.
In other words , if we are sure that our numeric value will remains numeric after conversion than we should use int.Parse method.
But if we are not sure our numeric value after conversion may or may not be "Null" than we should use Convert.ToInt32 Method,it works for Both numeric and Null value and never gives exception error.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Vijusah2010 on: 3/14/2013 | Points: 10
Convert.ToInt32 handles null values

Login to post response

More Interview Questions by Pradeep Raturi