I am working on a tool where I need to convert string values to their proper object types. E.g. convert a string like '2008-11-20T16:33:21Z' to a DateTime value. Numeric values like '42' and '42.42' must be converted to an Int32 value and a Double value respectively.
What is the best and most efficient approach to detect if a string is an integer or a number? Are Int32.TryParse or Double.TryParse the way to go?
In terms of efficiency, yes, TryParse is generally the preferred route.
If you can know (for example, by reflection) the target type in advance – but don’t want to have to use a big
switchblock, you might be interested in usingTypeConverter– for example: