Is there any significant advantages for converting a string to an integer value between int.Parse() and Convert.ToInt32() ?
string stringInt = "01234"; int iParse = int.Parse(stringInt); int iConvert = Convert.ToInt32(stringInt);
I found a question asking about casting vs Convert but I think this is different, right?
Convert. ToInt32 allows null value, it doesn't throw any errors Int. parse does not allow null value, and it throws an ArgumentNullException error.
Parse and Convert ToInt32 are two methods to convert a string to an integer. The main difference between int Parse and Convert ToInt32 in C# is that passing a null value to int Parse will throw an ArgumentNullException while passing a null value to Convert ToInt32 will give zero.
Thus int32 would be limited to a value between (-256^4/2) and (256^4/2-1). And int64 would be limited to a value between (-256^8/2) and (256^8/2-1).
Parse() and Int32. TryParse() can only convert strings. Convert. ToInt32() can take any class that implements IConvertible .
When passed a string as a parameter, Convert.ToInt32 calls int.Parse internally. So the only difference is an additional null check.
Here's the code from .NET Reflector
public static int ToInt32(string value) { if (value == null) { return 0; } return int.Parse(value, CultureInfo.CurrentCulture); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With