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 Passed | Int32.Parse(string) | Int32.TryParse(string, out int) | Convert.ToInt32(string) |
| exp1 | This will throw ArgumentNullException | This will return false and value 0 will be stored in int parameter | This will return 0 |
| exp2 | This will throw FormatException | This will return false and value 0 will be stored in int parameter | This will throw FormatException |
| exp3 | This will convert string to int and retrun 123 | This will convert string to int and return bool value. Value will stored in int parameter | This will convert string to int and retrun 123 |
| exp4 | The will throw OverflowException | This will return false and value 0 will be stored in int parameter | The 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.
Hello friends,
ReplyDeleteI 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.
This comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteOne 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
Hello Aviator,
ReplyDeleteLast part is really worth keeping in mind.
Nice sharing
ReplyDelete