This may seem a very trivial question but I'd always wondered whther it is bettor to call Convert.ToInt32 or Int32.Parse to convert a string to an integer number and which of theme is faster and better?
Reza Nassabeh www.professionalcsharp.com Don't forget to click Mark as Answer on the post that helped. That way future readers will know which post solved the issue.
Int32.Parse converts a string variable (or some other valid types) into a variable of type int. However if you pass a non-recognized value (text string, empty string or null string), it will
throw an exception (System.NullArgumentException if null else System.FormatException)
Convert.ToInt32 also converts a string (or some other valid types) into a variable of type int. It basically behaves the same way as Int32.Parse() except that it won't throw an exception System.NullArgumentException if a null string is passed to the method,
instead it will return 0.
You should therefore use the method that better suits your scenario.
Finally for those who are already using .Net 2.0, you may prefer to use the new Int32.TryParse method, which accepts a string, an ouput int and returns true or false if the conversion succeeds. If the conversion succeeds, the output int is set to the result.
I know the difference between Pars and TryPars but the link you provided has the answer,
"The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException."
Reza Nassabeh www.professionalcsharp.com Don't forget to click Mark as Answer on the post that helped. That way future readers will know which post solved the issue.
r_nassabeh
Contributor
3116 Points
505 Posts
Convert.ToInt32 vs Int32.Parse
Mar 07, 2008 06:16 PM|LINK
Hi
This may seem a very trivial question but I'd always wondered whther it is bettor to call Convert.ToInt32 or Int32.Parse to convert a string to an integer number and which of theme is faster and better?
www.professionalcsharp.com
Don't forget to click Mark as Answer on the post that helped. That way future readers will know which post solved the issue.
ecbruck
All-Star
98772 Points
9690 Posts
Moderator
MVP
Re: Convert.ToInt32 vs Int32.Parse
Mar 07, 2008 06:21 PM|LINK
If you know that the string can be parsed into an int, then use Int32.Parse. Otherwise, use Convert.ToInt32.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3689&SiteID=1
Microsoft MVP - ASP.NET
karan@dotnet
All-Star
26228 Points
4596 Posts
Re: Convert.ToInt32 vs Int32.Parse
Mar 07, 2008 06:32 PM|LINK
Int32.Parse converts a string variable (or some other valid types) into a variable of type int. However if you pass a non-recognized value (text string, empty string or null string), it will throw an exception (System.NullArgumentException if null else System.FormatException)
Convert.ToInt32 also converts a string (or some other valid types) into a variable of type int. It basically behaves the same way as Int32.Parse() except that it won't throw an exception System.NullArgumentException if a null string is passed to the method, instead it will return 0.
You should therefore use the method that better suits your scenario.
Finally for those who are already using .Net 2.0, you may prefer to use the new Int32.TryParse method, which accepts a string, an ouput int and returns true or false if the conversion succeeds. If the conversion succeeds, the output int is set to the result.
Karan
~ Blog ~
Remember To Mark The Post(s) That Helped You As The ANSWER
r_nassabeh
Contributor
3116 Points
505 Posts
Re: Convert.ToInt32 vs Int32.Parse
Mar 07, 2008 06:33 PM|LINK
Thanks ecbruck
I know the difference between Pars and TryPars but the link you provided has the answer, "The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException."
www.professionalcsharp.com
Don't forget to click Mark as Answer on the post that helped. That way future readers will know which post solved the issue.