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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Posted Jun 11, 2012, 8:26 PM EDT
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
Satyapriya NayakRank #47 · 31.5K reputationPosted Jun 11, 2012, 10:25 PM EDT
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