Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • int.Parse(), Convert.ToInt32() and int.TryParse()

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 624
    Comment on it

    I have seen three type of methods that are available in .NET to convert integer from string. Personally I feel, we can use int.TryParse(string s, out int).
    The main difference between int.Parse(), Convert.ToInt32() and int.TryParse()

    int.Parse(string str)
    This method converts the string to integer.
    If string str is either null, other than integer value or out of integer ranges, then it will throw ArgumentNullException, throw FormatException and throw OverflowException respectively.

    Convert.ToInt32(string str)
    This method converts the string to integer.
    If string str is either null, other than integer value or out of integer ranges, then it will return 0 rather than throw ArgumentNullException, throw FormatException and throw OverflowException respectively.

    int.TryParse(string str,Out)
    This method converts the string to integer out variable and returns true if successfully parsed, otherwise false.
    If string str is either null, other than integer value or out of integer ranges, then the out variable will have 0 rather than throw ArgumentNullException, FormatException and OverflowException respectively.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: