CsvReader Class
Reads CSV data from a text reader and parses it into rows.
public class CsvReaderExample
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
| Name | Description |
|---|---|
| CsvReader(TextReader) | Initializes a new instance of the CsvReader class with the specified TextReader. |
CsvReader(TextReader)
CsvReader.csInitializes a new instance of the CsvReader class with the specified TextReader.
public CsvReader(TextReader textReader)Parameters
textReaderTextReader- The
TextReaderto read CSV data from.
Exceptions
ArgumentNullExceptiontextReaderisnull.
Properties
| Name | Description |
|---|---|
| BaseReader | Gets the underlying TextReader used to read the CSV data. |
| ColumnNumber | Gets the number of characters read since the beginning of the current row. |
| EndOfStream | Gets a value indicating whether the end of the stream has been reached. |
| HasHeaderRow | Gets or sets a value indicating whether the first row contains column headers. |
| LineNumber | Gets the number of line terminators consumed so far. |
| Quote | Gets or sets the character used to quote values that contain special characters. Default is a double quote ("). |
| RowNumber | Gets or sets the current row number (0-based). |
| Separator | Gets or sets the character used to separate values in a row. Default is a comma (,). |
BaseReader
CsvReader.csGets the underlying TextReader used to read the CSV data.
public TextReader BaseReader { get; }Property value
ColumnNumber
CsvReader.csGets the number of characters read since the beginning of the current row.
public int ColumnNumber { get; }Property value
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.csGets a value indicating whether the end of the stream has been reached.
public bool EndOfStream { get; }Property value
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.csLineNumber
CsvReader.csGets the number of line terminators consumed so far.
public int LineNumber { get; }Property value
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.
Quote
CsvReader.csRowNumber
CsvReader.csSeparator
CsvReader.csMethods
| Name | Description |
|---|---|
| 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.csCreateRow(IReadOnlyList<CsvColumn>?, IReadOnlyList<string>)
CsvReader.csCreates a CsvRow instance. This method can be overridden to create custom row types.
protected virtual CsvRow CreateRow(IReadOnlyList<CsvColumn>? columns, IReadOnlyList<string> values)Parameters
columnsIReadOnlyList<CsvColumn>?- The columns of the row, or
nullif the CSV file has no header row. valuesIReadOnlyList<string>- The values of the row.
Returns
ReadRowAsync()
CsvReader.csConstants
| Name | Description |
|---|---|
| DefaultQuoteCharacter | The default quote character used in CSV files (double quote). |
| DefaultSeparatorCharacter | The 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 = ','