CsvWriter Class

Writes CSV data to a text writer.

public class CsvWriter

Example

using var writer = new StringWriter();
var csvWriter = new CsvWriter(writer) { EndOfLine = "\n" };

await csvWriter.WriteRowAsync("Name", "Age");
await csvWriter.WriteRowAsync("John", "30");
await csvWriter.WriteRowAsync("Jane", "25");

Assert.Equal("Name,Age\nJohn,30\nJane,25", writer.ToString());

Constructors

NameDescription
CsvWriter(TextWriter)Initializes a new instance of the CsvWriter class with the specified TextWriter.

CsvWriter(TextWriter)

CsvWriter.cs

Initializes a new instance of the CsvWriter class with the specified TextWriter.

public CsvWriter(TextWriter textReader)

Parameters

textReader TextWriter
The TextWriter to write CSV data to.

Exceptions

ArgumentNullException
textReader is null.

Properties

NameDescription
BaseWriterGets the underlying TextWriter used to write the CSV data.
EndOfLineGets or sets the end-of-line string. Default is Environment.NewLine.
QuoteGets or sets the character used to quote values that contain special characters. Default is a double quote (").
SeparatorGets or sets the character used to separate values in a row. Default is a comma (,).

BaseWriter

CsvWriter.cs

Gets the underlying TextWriter used to write the CSV data.

public TextWriter BaseWriter { get; }

Property value

TextWriter

EndOfLine

CsvWriter.cs

Gets or sets the end-of-line string. Default is Environment.NewLine.

public string EndOfLine { get; set; }

Property value

string

Gets or sets the character used to quote values that contain special characters. Default is a double quote (").

public char? Quote { get; set; }

Property value

char?

Separator

CsvWriter.cs

Gets or sets the character used to separate values in a row. Default is a comma (,).

public char Separator { get; set; }

Property value

char

Methods

NameDescription
BeginRowAsync()Begins a new row. This method must be called before writing values using CsvWriter.WriteValueAsync or CsvWriter.WriteValuesAsync.
WriteRowAsync(IEnumerable<string?>)Writes a complete row with the specified values.
WriteRowAsync(string?[])Writes a complete row with the specified values.
WriteValueAsync(string?)Writes a single value to the current row. Call CsvWriter.BeginRowAsync before calling this method.
WriteValuesAsync(IEnumerable<string?>)Writes multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method.
WriteValuesAsync(string?[])Writes multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method.

BeginRowAsync()

CsvWriter.cs

Begins a new row. This method must be called before writing values using CsvWriter.WriteValueAsync or CsvWriter.WriteValuesAsync.

public Task BeginRowAsync()

Returns

Task A task that represents the asynchronous operation.

WriteRowAsync overloads

WriteRowAsync(IEnumerable<string?>)

CsvWriter.cs

Writes a complete row with the specified values.

net10.0

public Task WriteRowAsync(IEnumerable<string?> values)

net11.0

public Task WriteRowAsync(IEnumerable<string?> values)
Parameters
values IEnumerable<string?>
The values to write.
Returns

Task A task that represents the asynchronous operation.

WriteRowAsync(string?[])

CsvWriter.cs

Writes a complete row with the specified values.

public Task WriteRowAsync(params string?[] values)
Parameters
values string?[]
The values to write.
Returns

Task A task that represents the asynchronous operation.

WriteValueAsync(string?)

CsvWriter.cs

Writes a single value to the current row. Call CsvWriter.BeginRowAsync before calling this method.

public Task WriteValueAsync(string? value)

Parameters

value string?
The value to write.

Returns

Task A task that represents the asynchronous operation.

WriteValuesAsync overloads

WriteValuesAsync(IEnumerable<string?>)

CsvWriter.cs

Writes multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method.

net10.0

public Task WriteValuesAsync(IEnumerable<string?> values)

net11.0

public Task WriteValuesAsync(IEnumerable<string?> values)
Parameters
values IEnumerable<string?>
The values to write.
Returns

Task A task that represents the asynchronous operation.

WriteValuesAsync(string?[])

CsvWriter.cs

Writes multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method.

public Task WriteValuesAsync(params string?[] values)
Parameters
values string?[]
The values to write.
Returns

Task A task that represents the asynchronous operation.