Utf8CsvWriter Class
Writes UTF-8 encoded CSV data.
public sealed class Utf8CsvWriter : IAsyncDisposable, IDisposableRemarks
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
| Name | Description |
|---|---|
| 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.csCreates a writer that writes UTF-8 CSV data to an IBufferWriter<T>.
public static Utf8CsvWriter Create(IBufferWriter<byte> bufferWriter, CsvWriterOptions? options = null)Parameters
bufferWriterIBufferWriter<byte>- The buffer writer to write the data to.
optionsCsvWriterOptions?- The options, or
nullto useCsvWriterOptions.Default.
Returns
Utf8CsvWriter The new writer.
Create(Stream, CsvWriterOptions?, bool)
Utf8CsvWriter.csCreates a writer that writes UTF-8 CSV data to a Stream.
public static Utf8CsvWriter Create(Stream stream, CsvWriterOptions? options = null, bool leaveOpen = false)Parameters
streamStream- The stream to write the data to.
optionsCsvWriterOptions?- The options, or
nullto useCsvWriterOptions.Default. leaveOpenbooltrueto leavestreamopen when theUtf8CsvWriteris disposed.
Returns
Utf8CsvWriter The new writer.
CreateToFile(string, CsvWriterOptions?)
Utf8CsvWriter.csCreates 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
pathstring- The path of the file.
optionsCsvWriterOptions?- The options, or
nullto useCsvWriterOptions.Default.
Returns
Utf8CsvWriter The new writer. Dispose it to flush the data and close the file.
Dispose()
Utf8CsvWriter.csSends 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.csEndRow()
Utf8CsvWriter.csEnds the current row, and sends the buffer to the underlying stream when it is mostly full.
public void EndRow()EndRowAsync(CancellationToken)
Utf8CsvWriter.csEnds the current row, and sends the buffer to the underlying stream asynchronously when it is mostly full.
public ValueTask EndRowAsync(CancellationToken cancellationToken = default)Parameters
cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
ValueTask A task that represents the asynchronous operation.
Flush()
Utf8CsvWriter.csSends the buffered data to the underlying stream and flushes it.
public void Flush()FlushAsync(CancellationToken)
Utf8CsvWriter.csSends the buffered data to the underlying stream and flushes it asynchronously.
public Task FlushAsync(CancellationToken cancellationToken = default)Parameters
cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
WriteField overloads
WriteField(ReadOnlySpan<byte>)
Utf8CsvWriter.csWrites a UTF-8 value in the current row.
public void WriteField(ReadOnlySpan<byte> utf8Value)Parameters
utf8ValueReadOnlySpan<byte>- The UTF-8 bytes of the value. They are written as they are.
WriteField(ReadOnlySpan<char>)
Utf8CsvWriter.csEncodes 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
valueReadOnlySpan<char>- The value.
WriteField(string?)
Utf8CsvWriter.csWriteField<T>(T, ReadOnlySpan<char>, IFormatProvider?)
Utf8CsvWriter.csFormats 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 : IUtf8SpanFormattableType parameters
T- The type of the value.
Parameters
valueT- The value.
formatReadOnlySpan<char>- The format string, or an empty span for the default format.
formatProviderIFormatProvider?- The format provider, or
nullto useCsvWriterOptions.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.csWrites values in the current row.
public void WriteFields(IEnumerable<string?> values)Parameters
valuesIEnumerable<string?>- The values.
nullis written as an empty value.
WriteFields(ReadOnlySpan<string?>)
Utf8CsvWriter.csWrites values in the current row.
public void WriteFields(params scoped ReadOnlySpan<string?> values)Parameters
valuesReadOnlySpan<string?>- The values.
nullis written as an empty value.
WriteRow overloads
WriteRow(IEnumerable<string?>)
Utf8CsvWriter.csWrites a row with the specified values.
public void WriteRow(IEnumerable<string?> values)Parameters
valuesIEnumerable<string?>- The values.
nullis written as an empty value.
WriteRow(ReadOnlySpan<string?>)
Utf8CsvWriter.csWrites a row with the specified values.
public void WriteRow(params scoped ReadOnlySpan<string?> values)Parameters
valuesReadOnlySpan<string?>- The values.
nullis written as an empty value.
WriteRowAsync(IEnumerable<string?>, CancellationToken)
Utf8CsvWriter.csWrites 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
valuesIEnumerable<string?>- The values.
nullis written as an empty value. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
ValueTask A task that represents the asynchronous operation.