Possible Duplicate:
Whats the main difference between int.Parse() and Convert.ToInt32
I would like to know what are PRO and CONS of using Convert.ToInt32 VS int.Parse.
Here an example of syntax I am using:
int myPageSize = Convert.ToInt32(uxPageSizeUsersSelector.SelectedValue);
int myPageSize = int.Parse(uxPageSizeUsersSelector.SelectedValue);
I also found out these articles, maybe they can help for a discussion:
Convert.ToInt32is for dealing with any object that implementsIConvertibleand can be converted to anint. Also,Convert.ToInt32returns0fornull, whileint.Parsethrows aArgumentNullException.int.Parseis specifically for dealing with strings.As it turns out, the
stringtype’sIConvertibleimplementation merely usesint.Parsein itsToInt32method.So effectively, if you call
Convert.ToIn32on astring, you are callingint.Parse, just with slightly more overhead (a couple more method calls).This is true for any conversion from
stringto some primitive type (they all callParse). So if you’re dealing with strongly-typedstringobjects (e.g., you’re parsing a text file), I’d recommendParse, simply because it’s more direct.Converting arbitrary objects (returned to you by some external library, for instance) is the scenario where I’d opt for using the
Convertclass.