Thursday, May 28, 2009

For Typecasting integer,How do we choose between (int), Int32.Parse and Convert.ToInt32

(int) can only typecast types which can be represented as integer like double, long, float, etc
Int32.Parse and Convert.ToInt32 both are similar only difference between them is if null is passed as parameter, Int32.Parse() will throw ArgumentNullException where as Convert.ToInt32 will return zero '0'

In .Net 2.0 method Int32.TryParse can be used for validating input for conversion. it returns false if it the input is null or invalid.

No comments: