about convert from string to int

T

tony

Hello!!


string number = "13";
int num;
Is it exactly the same if I use
num = Convert.ToInt32(number); OR
num = int.Parse(number)

Is it always in this case that I can choose whichever of int.Parse or
Convert.ToInt32 when I convert from a string containing numbers to an int.
So are these two interchangeable.

//Tony
 
G

Guest

Convert.ToInt32 returns zero if string is null reference, when int.Parse
throws ArgumentNullException
string number = "13";
int num;
Is it exactly the same if I use
num = Convert.ToInt32(number); OR
num = int.Parse(number)

Is it always in this case that I can choose whichever of int.Parse or
Convert.ToInt32 when I convert from a string containing numbers to an int.
So are these two interchangeable.

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
Is it always in this case that I can choose whichever of int.Parse or
Convert.ToInt32 when I convert from a string containing numbers to an int.
So are these two interchangeable.

Mostly, the difference is when the string is not a number as the other
posted said.

IIRC one of then use the other internally, This has been discussed here
several time, search in the archives to have more details , even a benchmark
of both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top