Friday, 30 April 2010

Difference between Int32.Parse(string), Int32.TryParse(string, out int) and Convert.ToInt32(string)


Hello Folks,

Today I am going to share with you guys my experiance with Casting methods. Which one to use and what is the
basic difference between them. I will discuss the difference between Int32.Parse(string)Int32.TryParse(string) and Convert.ToInt32(string)
and discuss on which to use

Int32.parse(string) and Convert.ToInt32(string) will either return value or throw exception.
Int32.TryParse(string, out int) will return true if conversion is success and false if not. And the converted value will be stored in 2nd parameter.

I will take a simple sample and display the behavior of these three methods
            string exp1 = null;
            string exp2 = "ASPDOTNETHACKER";
            string exp3 = "123";
            string exp4 = "12345678987654321";

Parameter PassedInt32.Parse(string)Int32.TryParse(string, out int)Convert.ToInt32(string)
exp1This will throw ArgumentNullExceptionThis will return false and value 0 will be stored in int parameterThis will return 0
exp2This will throw FormatExceptionThis will return false and value 0 will be stored in int parameterThis will throw FormatException
exp3This will convert string to int and retrun 123This will convert string to int and return bool value. Value will stored in int parameter This will convert string to int and retrun 123
exp4The will throw OverflowExceptionThis will return false and value 0 will be stored in int parameterThe will throw OverflowException

Now the million dollor question. which one to use.


Here goes the the order Int32.TryParse(string, out int) > Convert.ToInt32(string) > Int32.Parse(string)

Int32.Parse(string) does nothing for us unless we provide string representation of number. In all other case it throws exception. As one need to be extra careful while using Int32.Parse(string) avoid using it as far as possible.

Convert.ToInt32(string) is little better than Int32.Parse(string) as it shows some smartness to not throw exception if string is null and silently return 0. So Always prefer Convert.Int32(string) over Int32.Parse(string)

Int32.TryParse(string, out int) is the most robust and handle all the case and make sure no exception is thrown, whatever the case maybe. In other words this method is smartest of all. In fact I only use this method for casting purpose.  

5 comments:

  1. Hello friends,

    I just want to make one thing clear.
    The order which I display in the above post is with respect to robustness of the method and not with the performance.

    When it comes to performance(speed) order is

    Int32.Parse(string) > Convert.Int32(string) > Int32.TryParse(string, out int)

    Soon I am going to show you all how to use Visual Studio 2008 to compare performance.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,

    One more scenario:

    string exp5 = "";

    Convert.ToInt32(exp5);//This will throw FormatException


    Int32.Parse(exp5);//This will throw FormatException

    Int32.TryParse(exp5, out b);//This will return false and value 0 will be stored in int parameter


    Also, one important point to be noted here: When to use which Type Cast in case of storing the values in DB as e.g. Int32.TryParse(string, out int) will return 0 in case of any exception and 0 may result in incorrect result. So better to make a separate utility to return accordingly.

    Thanks,
    Aviator

    ReplyDelete
  4. Hello Aviator,

    Last part is really worth keeping in mind.

    ReplyDelete