What's the main difference between int.Parse() and Convert.ToInt32
- What is the main difference between
int.Parse()
andConvert.ToInt32()
? - Which one is to be preferred
Solution 1:
-
If you've got a string, and you expect it to always be an integer (say, if some web service is handing you an integer in string format), you'd use
Int32.Parse()
. -
If you're collecting input from a user, you'd generally use
Int32.TryParse()
, since it allows you more fine-grained control over the situation when the user enters invalid input. -
Convert.ToInt32()
takes an object as its argument. (See Chris S's answer for how it works)Convert.ToInt32()
also does not throwArgumentNullException
when its argument is null the wayInt32.Parse()
does. That also means thatConvert.ToInt32()
is probably a wee bit slower thanInt32.Parse()
, though in practice, unless you're doing a very large number of iterations in a loop, you'll never notice it.
Solution 2:
Have a look in reflector:
int.Parse("32"):
public static int Parse(string s)
{
return System.Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
which is a call to:
internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
{
byte* stackBuffer = stackalloc byte[1 * 0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
int num = 0;
StringToNumber(s, style, ref number, info, false);
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!HexNumberToInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
return num;
}
if (!NumberToInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
return num;
}
Convert.ToInt32("32"):
public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}
As the first (Dave M's) comment says.
Solution 3:
No difference as such.Convert.ToInt32()
calls int.Parse()
internally
Except for one thing Convert.ToInt32()
returns 0
when argument is null
Otherwise both work the same way
Solution 4:
int.Parse(string s)
- Integer in RANGE > returns integer value
- Null value > ArguementNullException
- Not in format > FormatException
- Value not in RANGE > OverflowException
Convert.ToInt32(string s)
- Integer in RANGE > returns integer value
- Null value > returns "0"
- Not in format > FormatException
- Value not in RANGE > OverflowException
bool isParsed = int.TryParse(string s,out res)
- Integer in RANGE > returns integer value, isParsed = true
- Null value > returns "0", isParsed = false
- Not in format > returns "0", isParsed = false
- Value not in RANGE > returns "0", isParsed = false
Try this code below.....
class Program
{
static void Main(string[] args)
{
string strInt = "24532";
string strNull = null;
string strWrongFrmt = "5.87";
string strAboveRange = "98765432123456";
int res;
try
{
// int.Parse() - TEST
res = int.Parse(strInt); // res = 24532
res = int.Parse(strNull); // System.ArgumentNullException
res = int.Parse(strWrongFrmt); // System.FormatException
res = int.Parse(strAboveRange); // System.OverflowException
// Convert.ToInt32(string s) - TEST
res = Convert.ToInt32(strInt); // res = 24532
res = Convert.ToInt32(strNull); // res = 0
res = Convert.ToInt32(strWrongFrmt); // System.FormatException
res = Convert.ToInt32(strAboveRange); //System.OverflowException
// int.TryParse(string s, out res) - Test
bool isParsed;
isParsed = int.TryParse(strInt, out res); // isParsed = true, res = 24532
isParsed = int.TryParse(strNull, out res); // isParsed = false, res = 0
isParsed = int.TryParse(strWrongFrmt, out res); // isParsed = false, res = 0
isParsed = int.TryParse(strAboveRange, out res); // isParsed = false, res = 0
}
catch(Exception e)
{
Console.WriteLine("Check this.\n" + e.Message);
}
}