Difference between Convert.ToInt 32 and Int.Parse
What is the difference between Convert.ToInt32 and Int.Parse?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jun 12, 2012, 2:25 AM
Both int.Parse and Convert.ToInt32 are used to convert string into the integer but Only difference between them is to Convert.ToInt32 handle null and returns '0' as output and int.parse is not going to handle NULL and will give a Argument Null Exception. Here is the example for that both are almost same except handling null.
string convertToInt = "12";
string nullString = null;
string maxValue = "32222222222222222222222222222222222";
string formatException = "12.32";
int parseResult;
// It will perfectly convert interger
parseResult= int.Parse(convertToInt);
// It will raise Argument Null Exception
parseResult= int.Parse(nullString);
//It willl raise Over Flow Exception
parseResult= int.Parse(maxValue);
//It will raise Format Exception
parseResult= int.Parse(formatException);
//For Convert.ToInt32
//It will perfectly convert integer
parseResult= Convert.ToInt32(convertToInt);
//It will ouput as 0 if Null string is there
parseResult= Convert.ToInt32(nullString);
//It will raise Over Flow Exception
parseResult= Convert.ToInt32(maxValue);
//It will raise Format Exception
parseResult= Convert.ToInt32(formatException);
Please refer the below links
http://www.codeproject.com/Articles/32885/Difference-Between-Int32-Parse-Convert-ToInt32-and
http://weblogs.asp.net/jalpeshpvadgama/archive/2010/11/26/difference-between-int-parse-and-convert-toint32.aspx
Thanks
Posted Jun 12, 2012, 12:26 AM
Convert.ToInt32() converts the string data to a proper true int32 value, and throws an exception if it can't convert the value.
Int.Parse parses the input to an integer value however it will throw an exception if it cant do it if the data provided is not numeric.
Instead of using Int.Parse, you try with Int.TryParse() method. The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails
http://msdn.microsoft.com/en-us/library/f02979c7.aspx