views:

142

answers:

4

Possible Duplicate:
Whats the main difference between int.Parse() and Convert.ToInt32

Hi guys, I would like to know what are PRO and CONS of using Convert.ToInt32 VS int.Parse. Thanks for you support!

Here an example of syntax I am using

            int myPageSize = Convert.ToInt32(uxPageSizeUsersSelector.SelectedValue);

            int myPageSize = int.Parse(uxPageSizeUsersSelector.SelectedValue);

I also found out this article, maybe can help for a discussion http://dotnetperls.com/int-parse http://aspdotnethacker.blogspot.com/2010/04/difference-between-int32parsestring.html http://aspdotnethacker.blogspot.com/p/visual-studio-performance-wizard.html

+1  A: 

The Convert interface is a more general purpose one. The net result should be the same though.

Internally, it just calls int.Parse:

public static int ToInt32(String value) {
    if (value == null)
        return 0;
    return Int32.Parse(value, CultureInfo.CurrentCulture);
}

Above code is from the reference source.

Pieter
+2  A: 

I cannot answer based on performance, but my preferred method is always int.tryparse(mystring, out myint) as that gives a clean failure that you can test for in the program flow (rather than doing a try/catch).

Moo
This really depends on what you want to do. If you really need the parsing to succeed, or you know that it is a valid representation of a number, it's totally valid to use int.Parse. What would you do when the TryParse fails in such a case? You would just throw another exception.
Pieter
In my use, *one* exception would be thrown, and that would be a meaningful exception to my application - in your usage, two or more exceptions would be thrown, the one or more thrown by the int.Parse and caught by you and the one you throw to add context to the errors passed back up.
Moo
+5  A: 

There's not much difference. Here's a quote found on msdn.

Basically the Convert class makes it easier to convert between all the base types.

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. MSDN

Kyle C
+2  A: 

Convert.ToInt32 is for dealing with any object that implements IConvertible and can be converted to an int.

int.Parse is specifically for dealing with strings.

As it turns out, the string type's IConvertible implementation merely uses int.Parse in its ToInt32 method.

So effectively, if you call Convert.ToIn32 on a string, you are calling int.Parse, just with slightly more overhead (a couple more method calls).

This is true for any conversion from string to some primitive type (they all call Parse). So if you're dealing with strongly-typed string objects (e.g., you're parsing a text file), I'd recommend Parse, simply because it's more direct.

Converting arbitrary objects (returned to you by some external library, for instance) is the scenario where I'd opt for using the Convert class.

Dan Tao