none
Difference between (int), Int32.Parse and Convert.ToInt32?

    Question

  • Hi,

      I have been wondering. In which scenario, I should be using (int), Int32.Parse and Convert.ToInt32?

      Or are they the same? I do notice in certain condition that i cannot cast directly using (int) and I have to use Convert.ToInt32.

      I am not that sure why i do that. I just need some confirmations from you all.

    Thank you.

    Cheers.

    Monday, April 25, 2005 1:05 AM

Answers

  • 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.

    It is really a matter of choice whichever you use.

    Also have a look at the new .NET 2.0 method Int32.TryParse, which attempts to convert a string to an int without throwing an exception.
    Monday, April 25, 2005 2:58 AM
  •  Chua Wen Ching wrote:
    Thanks for the replies.

    Now i understand. One more question.

    Is (int) same with Int32.Parse? So underneath of (int) is Int32.Parse?

    Thanks.


    No. (int) will only convert types that can be represented as an integer (ie double, long, float, etc) although some data loss may occur.

    Int32.Parse will only convert strings to integers.  You can't cast (ie (int)mystring) strings to integers.
    Monday, April 25, 2005 7:07 AM

All replies

  • Good question.

    Oddly enough, it appears that Convert.ToInt32() has a completely different stack trace than Int32.Parse().

    I wonder why the implementation is different?
    Monday, April 25, 2005 1:50 AM
  • 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.

    It is really a matter of choice whichever you use.

    Also have a look at the new .NET 2.0 method Int32.TryParse, which attempts to convert a string to an int without throwing an exception.
    Monday, April 25, 2005 2:58 AM
  • Thanks for the replies.

    Now i understand. One more question.

    Is (int) same with Int32.Parse? So underneath of (int) is Int32.Parse?

    Thanks.
    Monday, April 25, 2005 3:04 AM
  • Doh!

    It does help to be looking at the right method in Reflector.Tongue Tied

    Monday, April 25, 2005 3:06 AM
  •  Chua Wen Ching wrote:
    Thanks for the replies.

    Now i understand. One more question.

    Is (int) same with Int32.Parse? So underneath of (int) is Int32.Parse?

    Thanks.


    No. (int) will only convert types that can be represented as an integer (ie double, long, float, etc) although some data loss may occur.

    Int32.Parse will only convert strings to integers.  You can't cast (ie (int)mystring) strings to integers.
    Monday, April 25, 2005 7:07 AM
  • David,

      Thanks a lot. I understand now.

    Cheers.
    Monday, April 25, 2005 3:02 PM
  • Hi.

     

    Does anyone know what is the difference between int and integer in BizTalk 2006 ?

     

    I mean when i define some namespace  xmlns:int  versus  xmlns:integer  ???

     

     

    thnx.

    Sunday, September 16, 2007 5:02 PM
  • I just want to thanks David M.Kean for his nice clarifications about this point

     

    Thanks agian

     

    Mohamed

    Saturday, September 29, 2007 7:20 PM
  • I was experimenting with these 3 conversion methods and I ran into an issue using (int) with objects coming out of an ArrayList.  I have to use Convert.ToInt32, because the object being returned is of type "object { decimal }" (see code below).  Is this showing the "boxed" value?

    Code Block

                decimal myDecimalValue = 12312414.12M;

                //int implicitCast = myDecimalValue;            /* Compile Time Error */
                int explicitCast = (int)myDecimalValue;
                //int parseCast = Int32.Parse(myDecimalValue);  /* Compile Time Error */
                int convertCast = Convert.ToInt32(myDecimalValue);

                ArrayList myDecimalCollection = new ArrayList();
                myDecimalCollection.Add(myDecimalValue);

                //int int1 = (int)myDecimalCollection[0];      /* Run-Time Exception, cannot convert "Object{decimal} to int" */
                int int2 = Convert.ToInt32(myDecimalCollection[0]);



    Tuesday, October 02, 2007 4:12 PM
  • Wednesday, March 26, 2008 7:18 AM
  • I think int32.parse will throw an exception if the argument is null

     

     

    GOD BLESS U

    Friday, April 25, 2008 4:50 AM
  • Hello,

    Refer the link below good explanation of difference between the three.


    Also refer to this link to checkout which is fastest out of three

    Saturday, May 15, 2010 9:46 PM
  • 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.

    It is really a matter of choice whichever you use.

    Also have a look at the new .NET 2.0 method Int32.TryParse, which attempts to convert a string to an int without throwing an exception.

    thanks a lot for this clearification
    Tuesday, May 18, 2010 11:33 AM
  • A bit late but Thx anyway for this clearification!

     

    Ends

    Wednesday, July 21, 2010 1:07 PM