Int32.Parse (string val)
This method converts string representation of the integer into it's 32-bit signed integer.
val is null - throw ArgumentNullException.
val is not an integer - throw FormatException.
val is less than MinValue or greater thanMaxValue - throw OverflowException.string val1 = "356";
string val2 = "abc";
string val3 = null;
string val4 = "123456789123456789123456789123456789123456789";
int result;
result = Int32.Parse(val1); //-- 1234
result = Int32.Parse(val2); //-- FormatException
result = Int32.Parse(val3); //-- ArgumentNullException
result = Int32.Parse(val4); //-- OverflowException
Convert.ToInt32(string val)
This method converts string representation of the integer into it's 32-bit signed integer.
val is null - return 0.
val is not an integer - throw FormatException.
val is less than MinValue or greater thanMaxValue - throw OverflowException.
result = Int32.Parse(val1); //-- 1234
result = Int32.Parse(val2); //-- FormatException
result = Int32.Parse(val3); //-- 0
result = Int32.Parse(val4); //-- OverflowException
Int32.TryParse(string, out int)
This method converts string representation of the integer into it's 32-bit signed integer to a out variable and returns true if parsing succeed.
val is null - return 0.
val is not an integer - return 0.
val is less than MinValue or greater thanMaxValue - return 0.
bool result;
result = Int32.Parse(val1, out result); //-- true, result->1234
result = Int32.Parse(val2, result); //-- false, result->0
result = Int32.Parse(val3, result); //-- false, result->0
result = Int32.Parse(val4, result); //-- false, result->0
TryParse is the fastest method from above mentioned options.
No comments:
Write comments