1
Vote

The method ConvertTo<T>(this object value, T defaultValue, bool ignoreException) does not check if value is null

description

Please correct me if I'm wrong but the below method should check if value is null and if so return the defaultValue, exactly as in the other overload method ConvertTo<T>(this object value, T defaultValue)

Original code:
    public static T ConvertTo<T>(this object value, T defaultValue, bool ignoreException)
    {
        if (ignoreException)
        {
            try
            {
                return value.ConvertTo<T>();
            }
            catch
            {
                return defaultValue;
            }
        }
        return value.ConvertTo<T>();
    }
Modified code:
    public static T ConvertTo<T>(this object value, T defaultValue, bool ignoreException)
    {
        if (ignoreException)
        {
            try
            {
                return value.ConvertTo<T>(defaultValue);
            }
            catch
            {
                return defaultValue;
            }
        }
        return value.ConvertTo<T>(defaultValue);
    }

comments