CsvReader Class

Reads CSV data from a text reader and parses it into rows.

public class CsvReader

Example

using var reader = new StringReader("Name,Age\nJohn,30\nJane,25");
var csvReader = new CsvReader(reader) { HasHeaderRow = true };

var lines = new List<string>();
while (await csvReader.ReadRowAsync() is { } row)
{
    lines.Add($"{row["Name"]} is {row["Age"]} years old");
}

Assert.Equal(["John is 30 years old", "Jane is 25 years old"], lines);

Constructors

NameDescription
CsvReader(TextReader)Initializes a new instance of the CsvReader class with the specified TextReader.

CsvReader(TextReader)

CsvReader.cs

Initializes a new instance of the CsvReader class with the specified TextReader.

public CsvReader(TextReader textReader)

Parameters

textReader TextReader
The TextReader to read CSV data from.

Exceptions

ArgumentNullException
textReader is null.

Properties

NameDescription
BaseReaderGets the underlying TextReader used to read the CSV data.
ColumnNumberGets the number of characters read since the beginning of the current row.
EndOfStreamGets a value indicating whether the end of the stream has been reached.
HasHeaderRowGets or sets a value indicating whether the first row contains column headers.
LineNumberGets the number of line terminators consumed so far.
QuoteGets or sets the character used to quote values that contain special characters. Default is a double quote (").
RowNumberGets or sets the current row number (0-based).
SeparatorGets or sets the character used to separate values in a row. Default is a comma (,).

BaseReader

CsvReader.cs

Gets the underlying TextReader used to read the CSV data.

public TextReader BaseReader { get; }

Property value

TextReader

ColumnNumber

CsvReader.cs

Gets the number of characters read since the beginning of the current row.

public int ColumnNumber { get; }

Property value

int

Remarks

This is a character position, not the index of the field being read. It counts every character consumed by the current row, including the characters of a multi-line quoted value, and it is reset to 0 at the start of each row.

EndOfStream

CsvReader.cs

Gets a value indicating whether the end of the stream has been reached.

public bool EndOfStream { get; }

Property value

bool

Remarks

Reading this property can block: when the internal buffer is empty it queries the underlying TextReader synchronously, even though the rest of this type is asynchronous. Prefer testing whether CsvReader.ReadRowAsync returned null.

HasHeaderRow

CsvReader.cs

Gets or sets a value indicating whether the first row contains column headers.

public bool HasHeaderRow { get; set; }

Property value

bool

LineNumber

CsvReader.cs

Gets the number of line terminators consumed so far.

public int LineNumber { get; }

Property value

int

Remarks

This is 0 while the first line is being read, and is incremented as each line ends. Line terminators inside a quoted value are counted, so this tracks the physical line in the input rather than the number of rows returned. See CsvReader.RowNumber for the row.

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?

RowNumber

CsvReader.cs

Gets or sets the current row number (0-based).

public int RowNumber { get; set; }

Property value

int

Separator

CsvReader.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
CreateColumn(string, int)Creates a CsvColumn instance. This method can be overridden to create custom column types.
CreateRow(IReadOnlyList<CsvColumn>?, IReadOnlyList<string>)Creates a CsvRow instance. This method can be overridden to create custom row types.
ReadRowAsync()Reads the next row from the CSV data.

CreateColumn(string, int)

CsvReader.cs

Creates a CsvColumn instance. This method can be overridden to create custom column types.

protected virtual CsvColumn CreateColumn(string name, int index)

Parameters

name string
The name of the column.
index int
The zero-based index of the column.

Returns

CsvColumn A new CsvColumn instance.

CreateRow(IReadOnlyList<CsvColumn>?, IReadOnlyList<string>)

CsvReader.cs

Creates a CsvRow instance. This method can be overridden to create custom row types.

protected virtual CsvRow CreateRow(IReadOnlyList<CsvColumn>? columns, IReadOnlyList<string> values)

Parameters

columns IReadOnlyList<CsvColumn>?
The columns of the row, or null if the CSV file has no header row.
values IReadOnlyList<string>
The values of the row.

Returns

CsvRow A new CsvRow instance.

ReadRowAsync()

CsvReader.cs

Reads the next row from the CSV data.

net10.0

public Task<CsvRow?> ReadRowAsync()

net11.0

public Task<CsvRow?> ReadRowAsync()

Returns

Task<CsvRow?> A CsvRow representing the next row, or null if the end of the stream has been reached.

Constants

NameDescription
DefaultQuoteCharacterThe default quote character used in CSV files (double quote).
DefaultSeparatorCharacterThe default separator character used in CSV files (comma).

DefaultQuoteCharacter

The default quote character used in CSV files (double quote).

public const char DefaultQuoteCharacter = '"'

DefaultSeparatorCharacter

The default separator character used in CSV files (comma).

public const char DefaultSeparatorCharacter = ','