|
|
|
|
|
by MichaelGG
4839 days ago
|
|
TryParse returns a boolean. So you use it like this: int res;
if int.TryParse(s, out res) { // OK } { else // not ok }
You certainly do not need an exception to deal with the simple case of "did this string parse into an int".Edit: A great alternative signature is to use Maybe/Option, so you get Some int or None. match int.TryParse s with
| None -> ...
| Some i -> ...
|
|
int res = int.TryParse(s, 0); // 0 as the default if parsing fails
Of course if there is no reasonable default you should bubble the thing up anyway or handle it on the spot. But very often for input parsing there is sane default you can choose.