Searching for parseo, I found the following:
Int32.Parse(string);
Convert.ToInt32(string);
My question is the following:
What is the best way to parse, which is more effective and why. In what case is one form occupied, and in what case another?
I mentioned in a comment a while back that "They both do the same thing, except they throw different exceptions based on the value they 'parse'."
That said (source) , you have the following:
You can try a Fiddle here . :)
EDIT:
Convert.ToInt32()
has different overloads that allow you to cast practically anything toint
, howeverint.Parse()
you can only pass tostring
, throwing the respective exceptions mentioned above in each case, mentioning that itint.Parse()
has several overloads to specify the format of the number.EDIT 2:
In the end, deep down, both implementations call a called method
Number.Parse
that is internal to the .NET Framework, see the source code for both functions below 1 :That said, at the level of calls, yes, it may
Int32.Parse(string)
be faster, since it saves an internal call, not likeConvert.ToInt32(object)
, which first evaluates the object to convert and then calls theParse()
.1 : Source code of both implementations thanks to Reference Source See:
Convert.ToInt32(string)
eInt32.Parse(string)
.Both ways are valid, one difference I see is that Convert.ToInt32() allows you to assign various data types as a parameter
Convert.ToInt32
while the Int32.Parse() can only pass a string
Int32.Parse (String)
both are valid
If you have the
Int32.TryParse()
in case you want to validate the conversion without receiving an Exception