Class CsvWriter<T>
- Namespace
- FlameCsv
- Assembly
- FlameCsv.Core.dll
Provides convenience methods for writing CSV records.
public sealed class CsvWriter<T> : IDisposable, IAsyncDisposable where T : unmanaged, IBinaryInteger<T>
Type Parameters
T
- Inheritance
-
CsvWriter<T>
- Implements
Constructors
CsvWriter(CsvFieldWriter<T>)
Initializes a new writer instance.
public CsvWriter(CsvFieldWriter<T> inner)
Parameters
inner
CsvFieldWriter<T>Field writer instance to write to
Properties
AutoFlush
Whether to automatically check if the writer needs to be flushed after each record.
The default value is true
.
public bool AutoFlush { get; set; }
Property Value
- See Also
EnsureTrailingNewline
Whether to automatically ensure a trailing newline is written if not already present
when the writer completes without an error.
The default value is true
.
public bool EnsureTrailingNewline { get; set; }
Property Value
Remarks
The trailing newline check does not validate the field count.
ExpectedFieldCount
Field count required for each record, if set.
public int? ExpectedFieldCount { get; set; }
Property Value
- int?
Remarks
Set automatically after the first non-empty record if ValidateFieldCount
is true
.
FieldIndex
0-based index of the current field. Reset to 0 at the start of each record.
public int FieldIndex { get; }
Property Value
IsCompleted
Whether the writer has completed (disposed).
public bool IsCompleted { get; }
Property Value
LineIndex
1-based index of the current line/record. Incremented after each newline.
public int LineIndex { get; }
Property Value
Remarks
Newlines in quoted fields/strings are not counted, this property represents the logical CSV record index.
Options
Options instance of this writer.
public CsvOptions<T> Options { get; }
Property Value
- CsvOptions<T>
Methods
Complete(Exception?)
Completes the writer, flushing any remaining data if exception
is null.
Multiple completions are no-ops.
public void Complete(Exception? exception = null)
Parameters
exception
ExceptionObserved exception when writing the data. If not null, the final buffer is not flushed and the exception is rethrown.
CompleteAsync(Exception?, CancellationToken)
Completes the writer, flushing any remaining data if exception
is null.
Multiple completions are no-ops.
public ValueTask CompleteAsync(Exception? exception = null, CancellationToken cancellationToken = default)
Parameters
exception
ExceptionObserved exception when writing the data, passed to the inner ICsvBufferWriter<T>. If not null, the final buffer is not flushed and the exception is rethrown.
cancellationToken
CancellationTokenToken to cancel the operation
Returns
Flush()
Flushes the writer.
public void Flush()
Exceptions
- ObjectDisposedException
Thrown if the writer has completed (see Complete(Exception?)).
FlushAsync(CancellationToken)
Flushes the writer.
public ValueTask FlushAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenToken to cancel flushing
Returns
Exceptions
- ObjectDisposedException
Thrown if the writer has completed (see CompleteAsync(Exception?, CancellationToken)).
NextRecord()
Writes a newline and flushes the buffer if needed when AutoFlush is true.
public void NextRecord()
Exceptions
- ObjectDisposedException
The writer has completed
NextRecordAsync(CancellationToken)
Writes a newline and flushes the buffer if needed when AutoFlush is true.
public ValueTask NextRecordAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenToken to cancel the flush
Returns
Exceptions
- ObjectDisposedException
The writer has completed
WriteField(ReadOnlySpan<char>, bool)
Writes a field with the preceding delimiter if needed.
public void WriteField(ReadOnlySpan<char> text, bool skipEscaping = false)
Parameters
text
ReadOnlySpan<char>Value to write
skipEscaping
boolWhether no escaping should be performed, use with care
WriteField(ReadOnlySpan<T>, bool)
Writes a field with the preceding delimiter if needed.
public void WriteField(ReadOnlySpan<T> text, bool skipEscaping = false)
Parameters
text
ReadOnlySpan<T>Value to write
skipEscaping
boolWhether no escaping should be performed, use with care
WriteField<TField>(CsvConverter<T, TField>, TField?)
Writes a field with the preceding delimiter if needed.
public void WriteField<TField>(CsvConverter<T, TField> converter, TField? value)
Parameters
converter
CsvConverter<T, TField>Converter instance to write the value with
value
TFieldValue to write
Type Parameters
TField
Field type that will be converted
WriteField<TField>(TField?)
Writes a field with the preceding delimiter if needed.
[RequiresDynamicCode("This code path may require types that cannot be statically analyzed and might need runtime code generation. Use an alternative overload for native AOT applications.")]
[RequiresUnreferencedCode("This code path may require types that cannot be statically analyzed and might need runtime code generation. Use an alternative overload for native AOT applications.")]
public void WriteField<TField>(TField? value)
Parameters
value
TFieldValue to write
Type Parameters
TField
Field type that will be converted
Remarks
Converter will be retrieved from options.
WriteHeader(scoped ReadOnlySpan<string>)
Writes the provided header values.
public int WriteHeader(scoped ReadOnlySpan<string> values)
Parameters
values
ReadOnlySpan<string>Header values
Returns
- int
Number of fields written
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteHeader<TRecord>()
Writes the header for TRecord
to the current line using TypeBinder.
[RequiresUnreferencedCode("This code path uses reflection. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
[RequiresDynamicCode("This code path uses compiled expressions. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
public int WriteHeader<TRecord>()
Returns
- int
Number of fields written
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteHeader<TRecord>(CsvTypeMap<T, TRecord>)
Writes the header for TRecord
to the current line using the type map.
public int WriteHeader<TRecord>(CsvTypeMap<T, TRecord> typeMap)
Parameters
typeMap
CsvTypeMap<T, TRecord>Type map to use for writing
Returns
- int
Number of fields written
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteRaw(ReadOnlySpan<T>, int, int)
Writes a sequence of raw characters to the writer.
FieldIndex and LineIndex are not tracked automatically, and no escaping is performed.
This is equivalent to writing the value directly to the inner buffer writer.
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void WriteRaw(ReadOnlySpan<T> value, int fieldsWritten = 0, int linesWritten = 0)
Parameters
value
ReadOnlySpan<T>Value to write
fieldsWritten
intHow many fields the value spans
linesWritten
intHow many new lines the value spans
WriteRecord<TRecord>(CsvTypeMap<T, TRecord>, TRecord)
Writes the value to the current line using the type map.
public int WriteRecord<TRecord>(CsvTypeMap<T, TRecord> typeMap, TRecord value)
Parameters
typeMap
CsvTypeMap<T, TRecord>Type map to use for writing
value
TRecordValue to write
Returns
- int
Number of fields written
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteRecord<TRecord>(TRecord)
Writes the value to the current line using TypeBinder.
[RequiresUnreferencedCode("This code path uses reflection. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
[RequiresDynamicCode("This code path uses compiled expressions. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
public int WriteRecord<TRecord>(TRecord value)
Parameters
value
TRecordValue to write
Returns
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).