Conversion using ‘Convert.Int32 vs Int32.Parse’ operator
Basically the Convert class makes it easier to convert between all the base types. Ingternally, the Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse.
However the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException.
Below are results with various type of inputs to Convert.Int32()
Input | Convert.Int32() Results |
Null | 0 |
Empty string (“”) | Format Exception |
Alphabet | Format Exception |
Numeric | Sucessfully Conversion |
You can see, except null and numeric input, Convert.Int32(), throws an exception. Hence to avoid it better to check whether the conversion can be made or not. Here is the post to how to do it.
Thanks & Regards,
Arun Manglick || Senior Tech
No comments:
Post a Comment