I need my string-to-int converter to handle all exceptions without throwing any errors. I also have a use case where I need the default value to be less than zero if the TryParse
is false.
- Is there a built-in way of doing this?
- Should I make any changes to the method?
public int ToInt32OrDefault(string value, int defaultValue = 0)
{
int result;
if (Int32.TryParse(value, out result) == false)
result = defaultValue;
return result;
}