Difference between (int), Int32.Parse and Convert.ToInt32?
-
Monday, April 25, 2005 1:05 AM
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.
All Replies
-
Monday, April 25, 2005 1:50 AMGood 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 2:58 AMModerator
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.- Proposed As Answer by David M. KeanMicrosoft Employee, Moderator Wednesday, June 04, 2008 5:37 AM
- Marked As Answer by David M. KeanMicrosoft Employee, Moderator Saturday, June 07, 2008 3:21 PM
-
Monday, April 25, 2005 3:04 AMThanks 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:06 AMDoh!
It does help to be looking at the right method in Reflector.
-
Monday, April 25, 2005 7:07 AMModerator
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.- Proposed As Answer by David M. KeanMicrosoft Employee, Moderator Wednesday, June 04, 2008 5:37 AM
-
Monday, April 25, 2005 3:02 PMDavid,
Thanks a lot. I understand now.
Cheers. -
Sunday, September 16, 2007 5: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.
-
Saturday, September 29, 2007 7:20 PM
I just want to thanks David M.Kean for his nice clarifications about this point
Thanks agian
Mohamed
-
Tuesday, October 02, 2007 4:12 PMI 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 Blockdecimal 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]); -
Wednesday, March 26, 2008 7:18 AM
check out this post: Why Convert.ToInt32 Might be Dangerous .
-
Friday, April 25, 2008 4:50 AM
I think int32.parse will throw an exception if the argument is null
GOD BLESS U
-
Saturday, May 15, 2010 9:46 PMHello,
Refer the link below good explanation of difference between the three.
Also refer to this link to checkout which is fastest out of three
-
Tuesday, May 18, 2010 11:33 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.
thanks a lot for this clearification -
Wednesday, July 21, 2010 1:07 PM
A bit late but Thx anyway for this clearification!
Ends

