Convert string to integer or default 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;
}
Use int rather than Int32
You can write an inline expression rather than a multi-line statement


I would prefer to write an extension method here to wrap this method:

public static class IntExtension
{
    public static int ToInt32OrDefault(this string value,int defaultValue=0)
    {
        int result;
        return int.TryParse(value, out result) ? result : defaultValue;
    }
}