Utf8CsvWriter Class

Writes UTF-8 encoded CSV data.

public sealed class Utf8CsvWriter : IAsyncDisposable, IDisposable

Remarks

Write the values of a row with the WriteField methods, then end the row with Utf8CsvWriter.EndRow or Utf8CsvWriter.EndRowAsync. Every row ends with CsvWriterOptions.NewLine, including the last one.

The writer keeps the data in a buffer. Utf8CsvWriter.EndRow sends the buffer to the underlying stream when it is mostly full; the WriteField methods never do. Utf8CsvWriter.EndRowAsync sends it asynchronously, so a writer used only with the asynchronous methods never blocks. Call Utf8CsvWriter.Flush or Utf8CsvWriter.FlushAsync, or dispose the writer, to send the rest of the data.

The separator and the quote characters must be ASCII characters. The writer does not write a byte order mark.

This type is not thread-safe.

Example

using var stream = new MemoryStream();
using (var writer = Utf8CsvWriter.Create(stream, leaveOpen: true))
{
    writer.WriteRow("Product", "Price");
    writer.WriteField("Café"u8); // UTF-8 bytes are written as they are
    writer.WriteField(3.5m, "F2");
    writer.EndRow();
}

Assert.Equal("Product,Price\r\nCafé,3.50\r\n", Encoding.UTF8.GetString(stream.ToArray()));
Implements

Methods

NameDescription
Create(IBufferWriter<byte>, CsvWriterOptions?)Creates a writer that writes UTF-8 CSV data to an IBufferWriter<T>.
Create(Stream, CsvWriterOptions?, bool)Creates a writer that writes UTF-8 CSV data to a Stream.
CreateToFile(string, CsvWriterOptions?)Creates a writer that writes UTF-8 CSV data to a file. The file is created or overwritten.
Dispose()Sends the buffered data to the underlying stream, and disposes it unless the writer was created with leaveOpen.
DisposeAsync()Sends the buffered data to the underlying stream asynchronously, and disposes it unless the writer was created with leaveOpen.
EndRow()Ends the current row, and sends the buffer to the underlying stream when it is mostly full.
EndRowAsync(CancellationToken)Ends the current row, and sends the buffer to the underlying stream asynchronously when it is mostly full.
Flush()Sends the buffered data to the underlying stream and flushes it.
FlushAsync(CancellationToken)Sends the buffered data to the underlying stream and flushes it asynchronously.
WriteField(ReadOnlySpan<byte>)Writes a UTF-8 value in the current row.
WriteField(ReadOnlySpan<char>)Encodes a value to UTF-8 and writes it in the current row. Invalid UTF-16 sequences are replaced with U+FFFD.
WriteField(string?)Encodes a value to UTF-8 and writes it in the current row.
WriteField<T>(T, ReadOnlySpan<char>, IFormatProvider?)Formats a value as UTF-8 with CsvWriterOptions.FormatProvider and writes it in the current row.
WriteFields(IEnumerable<string?>)Writes values in the current row.
WriteFields(ReadOnlySpan<string?>)Writes values in the current row.
WriteRow(IEnumerable<string?>)Writes a row with the specified values.
WriteRow(ReadOnlySpan<string?>)Writes a row with the specified values.
WriteRowAsync(IEnumerable<string?>, CancellationToken)Writes a row with the specified values, and sends the buffer to the underlying stream asynchronously when it is mostly full.

Create overloads

Create(IBufferWriter<byte>, CsvWriterOptions?)

Utf8CsvWriter.cs

Creates a writer that writes UTF-8 CSV data to an IBufferWriter<T>.

public static Utf8CsvWriter Create(IBufferWriter<byte> bufferWriter, CsvWriterOptions? options = null)
Parameters
bufferWriter IBufferWriter<byte>
The buffer writer to write the data to.
options CsvWriterOptions?
The options, or null to use CsvWriterOptions.Default.
Returns

Utf8CsvWriter The new writer.

Create(Stream, CsvWriterOptions?, bool)

Utf8CsvWriter.cs

Creates a writer that writes UTF-8 CSV data to a Stream.

public static Utf8CsvWriter Create(Stream stream, CsvWriterOptions? options = null, bool leaveOpen = false)
Parameters
stream Stream
The stream to write the data to.
options CsvWriterOptions?
The options, or null to use CsvWriterOptions.Default.
leaveOpen bool
true to leave stream open when the Utf8CsvWriter is disposed.
Returns

Utf8CsvWriter The new writer.

CreateToFile(string, CsvWriterOptions?)

Utf8CsvWriter.cs

Creates a writer that writes UTF-8 CSV data to a file. The file is created or overwritten.

public static Utf8CsvWriter CreateToFile(string path, CsvWriterOptions? options = null)

Parameters

path string
The path of the file.
options CsvWriterOptions?
The options, or null to use CsvWriterOptions.Default.

Returns

Utf8CsvWriter The new writer. Dispose it to flush the data and close the file.

Sends the buffered data to the underlying stream, and disposes it unless the writer was created with leaveOpen.

public void Dispose()

Remarks

A row that is not ended is written without its line terminator.

DisposeAsync()

Utf8CsvWriter.cs

Sends the buffered data to the underlying stream asynchronously, and disposes it unless the writer was created with leaveOpen.

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous operation.

Remarks

A row that is not ended is written without its line terminator.

Ends the current row, and sends the buffer to the underlying stream when it is mostly full.

public void EndRow()

EndRowAsync(CancellationToken)

Utf8CsvWriter.cs

Ends the current row, and sends the buffer to the underlying stream asynchronously when it is mostly full.

public ValueTask EndRowAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

ValueTask A task that represents the asynchronous operation.

Sends the buffered data to the underlying stream and flushes it.

public void Flush()

FlushAsync(CancellationToken)

Utf8CsvWriter.cs

Sends the buffered data to the underlying stream and flushes it asynchronously.

public Task FlushAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

Task A task that represents the asynchronous operation.

WriteField overloads

WriteField(ReadOnlySpan<byte>)

Utf8CsvWriter.cs

Writes a UTF-8 value in the current row.

public void WriteField(ReadOnlySpan<byte> utf8Value)
Parameters
utf8Value ReadOnlySpan<byte>
The UTF-8 bytes of the value. They are written as they are.

WriteField(ReadOnlySpan<char>)

Utf8CsvWriter.cs

Encodes a value to UTF-8 and writes it in the current row. Invalid UTF-16 sequences are replaced with U+FFFD.

public void WriteField(ReadOnlySpan<char> value)
Parameters
value ReadOnlySpan<char>
The value.

WriteField(string?)

Utf8CsvWriter.cs

Encodes a value to UTF-8 and writes it in the current row.

public void WriteField(string? value)
Parameters
value string?
The value. null is written as an empty value.

WriteField<T>(T, ReadOnlySpan<char>, IFormatProvider?)

Utf8CsvWriter.cs

Formats a value as UTF-8 with CsvWriterOptions.FormatProvider and writes it in the current row.

public void WriteField<T>(T value, ReadOnlySpan<char> format = default, IFormatProvider? formatProvider = null) where T : IUtf8SpanFormattable
Type parameters
T
The type of the value.
Parameters
value T
The value.
format ReadOnlySpan<char>
The format string, or an empty span for the default format.
formatProvider IFormatProvider?
The format provider, or null to use CsvWriterOptions.FormatProvider.
Remarks

The value is formatted directly in the buffer of the writer, without allocating a string. CsvWriterOptions.EscapeFormulas does not apply.

WriteFields overloads

WriteFields(IEnumerable<string?>)

Utf8CsvWriter.cs

Writes values in the current row.

public void WriteFields(IEnumerable<string?> values)
Parameters
values IEnumerable<string?>
The values. null is written as an empty value.

WriteFields(ReadOnlySpan<string?>)

Utf8CsvWriter.cs

Writes values in the current row.

public void WriteFields(params scoped ReadOnlySpan<string?> values)
Parameters
values ReadOnlySpan<string?>
The values. null is written as an empty value.

WriteRow overloads

WriteRow(IEnumerable<string?>)

Utf8CsvWriter.cs

Writes a row with the specified values.

public void WriteRow(IEnumerable<string?> values)
Parameters
values IEnumerable<string?>
The values. null is written as an empty value.

WriteRow(ReadOnlySpan<string?>)

Utf8CsvWriter.cs

Writes a row with the specified values.

public void WriteRow(params scoped ReadOnlySpan<string?> values)
Parameters
values ReadOnlySpan<string?>
The values. null is written as an empty value.

WriteRowAsync(IEnumerable<string?>, CancellationToken)

Utf8CsvWriter.cs

Writes a row with the specified values, and sends the buffer to the underlying stream asynchronously when it is mostly full.

public ValueTask WriteRowAsync(IEnumerable<string?> values, CancellationToken cancellationToken = default)

Parameters

values IEnumerable<string?>
The values. null is written as an empty value.
cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

ValueTask A task that represents the asynchronous operation.