Trouble with CSV-Helper not converting bool values

The boolean values overload for TypeConverterOption is used only when reading. It allows you to specify multiple values that can be used for true/false when reading. So you could do 1, "true", "TRUE", "True", "yes", etc.

Currently the only way to do it when writing is to create a custom type converter.

public class MyBooleanConverter : DefaultTypeConverter
{
    public override string ConvertToString( TypeConverterOptions options, object value )
    {
        if( value == null )
        {
            return string.Empty;
        }

        var boolValue = (bool)value;

        return boolValue ? "yes" : "no";
    }
}

You can then apply it to all booleans globally.

CsvHelper.TypeConversion.TypeConverterFactory.AddConverter<bool>( new MyBooleanConverter() );

Or apply it to a single property via the mapping.

Map( m => m.MyBoolProperty ).TypeConverter<MyBooleanConverter>();

The accepted solution doesn't work with latest versions. I am using CsvHelper 15.0.0 and this is how it works.

public class MyBooleanConverter : DefaultTypeConverter
{
    public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
    {
        if( value == null )
        {
            return string.Empty;
        }
        var boolValue = (bool)value;
        return boolValue ? "yes" : "no";
    }
}

Applying to single property:

Map(x => x.IsValid).Index(3).TypeConverter<MyBooleanConverter>();

Applying from configuration of reader/writter:

using var csvWriter = new CsvWriter(new StreamWriter(path), CultureInfo.InvariantCulture);
csvWriter.Configuration.TypeConverterCache.AddConverter<bool>(new MyBooleanConverter());
csvWriter.WriteRecords(data);