Open E-School

Male
Friday, February 24, 1984

CSharp (C#) - Convert.ToInt32 vs int.Parse vs int.TryParse. Convert a string to integer using C#

Category: Programming Date: Wednesday, March 25, 2015

Goal

To understand the difference between Conver.ToInt32 , int.Parse and int.TryParse methods and understand which method to use in which situation

Convert.ToInt32

This method has 19 overloads. This means that it can take input in many different types and will convert it into an integer. Following cases are of importance

  1. If the input is convertible into an integer it will do so successfully.
  2. If the input is null it will not throw error and will return 0
  3. If the input is not null and not convertible to an integer then it will throw a FormatException

First example

string str = "123";    int number = Convert.ToInt32(str);

Here Convert.ToInt32 will successfully convert str in its integer equivalent

Second example

string str = null;  int number = Convert.ToInt32(str);

Here Convert.ToInt32 will return a 0. No exception will be thrown

Third example

string str = "abc";  int number = Convert.ToInt32(str);

Here Conver.ToInt32 will throw a FormatException as the string is not convertible to integer

Int.Parse

Int.Parse method only takes a string input as compared to Convert.ToInt32 which takes inputs in many different base types. Following cases are of importance

  1. If the input is convertible into an integer it will do so successfully
  2. If the input is null it will throw an ArgumentNullException
  3. If the input is not null and not convertible to an integer then it will throw a FormatException

First example

string str = "123";  int number = int.Parse(str);

Here int.Parse will successfully convert str in its integer equivalent

Second example

string str = null;  int number = int.Parse(str);

Here int.Parse will will throw an ArgumentNullException unlike Conver.ToInt32 which would have simply returned a 0

Third example

string str = "abc";  int number = Convert.ToInt32(str);

Here int.Parse will throw a FormatException as the string is not convertible to integer

Int.TryParse

Int.TryParse takes only string inputs. But the good part is that if the input is not convertible to an integer then it will not throw any error. It will simply return a Boolean telling that it cannot convert the input into an integer. This means that int.TryParse will never throw a runtime exception. It will either convert the input into an integer or return false indicating that it could not do the conversion.

Important : int.TryParse will never throw a runtime exception.

Example usage

string str = "123";    int number;  bool success = int.TryParse(str, out number);

Here the result of conversion will be written to the number variable and the value of success will be true.

Extra Remarks

Whenever possible try to int.TryParse as it has the minimum chances of throwing runtime exceptions in case the input is not convertible to an integer.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp