intID1 = Int32.Parse(myValue.ToString()); intID2 = Convert.ToInt32(myValue);
Which one is better and why?
Parse() and Int32. TryParse() can only convert strings. Convert. ToInt32() can take any class that implements IConvertible .
Parse(String) Method is used to convert the string representation of a number to its 32-bit signed integer equivalent. Syntax: public static int Parse (string str); Here, str is a string that contains a number to convert.
TOInt32() and Int32. Parse() is efficient? 1) Int32.
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).
They are exactly the same, except that Convert.ToInt32(null)
returns 0
.
Convert.ToInt32
is defined as follows:
public static int ToInt32(String value) { if (value == null) return 0; return Int32.Parse(value, CultureInfo.CurrentCulture); }
Well, Reflector says...
public static int ToInt32(string value) { if (value == null) { return 0; } return int.Parse(value, CultureInfo.CurrentCulture); } public static int Parse(string s) { return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo); }
So they're basically the same except that Convert.ToInt32()
does an added null check.
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