Hi all,
Can any one please explain what is the difference between
Convert.ToInt32(somevalue),int.Parse(somevalue) and
(int)somevalue
thanks in Advance
thomson
Assuming that somevalue is a string since Parse requires a string, the
implementation is slightly different between the first two.
Convert.ToInt32(string somevalue) first checks to see if the string is
null. If the passed in string is null, Convert.ToInt32 will return 0. If
the passed in string is not null, Convert.ToInt32 calls Int32.Parse,
passing it the string that was passed to Convert.ToInt32.
So getting rid of the first assumption that somevalue is a string, another
major difference is that there are overloads of Convert.ToInt32 to handle a
wide variety of data types whereas Int32.Parse can only handle strings.
Finally with a straight cast, you are limited to casting only what can be a
valid Int32 as determined at compile time. You cannot cast a string to an
int, it won't compile. You can cast other numeric types to decimal,
although it might throw a runtime exception if there is an overflow.