Hi Guys
NP61 Any advantage
int volInt = Int32.Parse(volString);
int volInt = Convert.ToInt32(volString);
In a program these two codes are producing similar output. I wish to know whether
these codes are same or is there any advantage one over other when using it. Anyone knows please explain.
Thank you
Posted Nov 25, 2007, 10:21 AM
Thank you Alan, for the explanation.
AlanPosted Nov 25, 2007, 10:13 AM
Convert.ToInt32() does in fact call Int32.Parse() 'under the hood' if the string is non-null. If the string is null, Convert.ToInt32() returns zero whereas Int32.Parse() throws an exception.
So, if you're certain that the string won't be null, use Int32.Parse() - or simply int.Parse() - to save the overhead of an extra function call. Otherwise use Convert.ToInt32() if a return value of zero is acceptable in these circumstances.