is Double.TryParse a better way or use Regular Expression, and why ?
Printable View
is Double.TryParse a better way or use Regular Expression, and why ?
Double.TryParse performs the conversion for you, rather than just telling you whether a conversion is possible. Having said that, it will produce a Double that must still be cobverted to an Integer. Also, TryParse allows you to easily specify the types of numbers that are valid where a regular expression would start to get quite complex. .NET 2.0 has a TryParse method for every numeric type. Finally, using a TryParse method is more self-documenting than using a Regex, which is a more generic operation and you'd have to examine the pattern itself to see exactly what it was doing. In the grand scheme of things I doubt that there's much difference, but TryParse is more specific to the task so I would use it for that reason alone.