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

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.




Answer this question

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

  • Smoky

    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]);




  • Bob Cohen

    Thanks for the replies.

    Now i understand. One more question.

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

    Thanks.

  • mkrajew

    I just want to thanks David M.Kean for his nice clarifications about this point

    Thanks agian

    Mohamed



  • Abbott

     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.


  • kuponutcom

    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.

  • mannis

    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

  • RichardPro

    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.



  • mark_k

    David,

      Thanks a lot. I understand now.

    Cheers.

  • Notis

    Doh!

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


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