public sealed record CsvReaderOptions
Namespace
CsvHelper
Assembly
CsvHelper.dll
Inheritance
object CsvReaderOptions
Builds
net10.0 netstandard2.1 netstandard2.0 net47

Constructors

Properties

AllowLineBreaksInQuotes

Added in v34.0.0
public bool AllowLineBreaksInQuotes { get; init; }

Whether quoted fields may contain line breaks (RFC 4180). Defaults to true. Set to false to recover from files with unbalanced quotes: every line break then ends the record, so one bad quote cannot swallow the rest of the file.

Property value

bool

View source

BufferSize

Added in v34.0.0
public int BufferSize { get; init; }

The size in bytes of the read buffer. Defaults to 64 KiB. Grows automatically for long records.

Property value

int

View source

CommentPrefix

Added in v34.0.0
public char? CommentPrefix { get; init; }

When set, lines that start with this character are skipped as comments. Defaults to null (no comments). Must be an ASCII character.

Property value

char?

View source

DetectSeparatorHint

Added in v34.0.0
public bool DetectSeparatorHint { get; init; }

Whether a first line of the form sep=; (written by Excel) sets the separator and is skipped. Only applies when Separator is not set. Defaults to true.

Property value

bool

View source

Encoding

Added in v34.0.0
public Encoding? Encoding { get; init; }

The encoding of stream input. When null (the default), byte order marks are honored and UTF-8 is assumed otherwise. Non UTF-8 input is transcoded to UTF-8 while reading.

Property value

View source

Escape

Added in v34.0.0
public char Escape { get; init; }

The escape character of CsvMode.Escape mode. Defaults to \. Must be an ASCII character other than space and tab.

Property value

char

View source

HasHeader

Added in v34.0.0
public bool HasHeader { get; init; }

Whether the first record is a header row. Defaults to true.

Property value

bool

View source

IncludeValuesInExceptions

Added in v34.0.0
public bool IncludeValuesInExceptions { get; init; }

Whether exception messages contain field values, such as 'x' is not a valid boolean. Defaults to true. Set to false to keep values, which may be personal data, out of logs: the messages of Parse, ParseExact and the generated record mappers then name the type but not the value. Exceptions of your own parsers and converters are not changed.

Property value

bool

View source

MaxRecordSize

Added in v34.0.0
public int? MaxRecordSize { get; init; }

The largest record, in bytes of UTF-8 input, that the reader grows its buffer for, which guards against hostile input such as an unclosed quote at the start of a large file. Defaults to null: records grow the buffer up to about 2 GiB. Checked only when the reader needs more input, so it costs nothing per record: a record that is not complete yet and longer than the limit throws a CsvFormatException, while a longer record that is already in the buffer (see BufferSize) is read, and the buffer never grows past about twice the limit. In-memory input (a byte array or ReadOnlyMemory<T>) is not buffered and not checked.

Property value

int?

View source

NormalizeHeaderName

Added in v34.0.0
public Func<string, string>? NormalizeHeaderName { get; init; }

Changes header names before they are matched, both those of the input and those that are looked up (by CsvReader.GetFieldIndex(string), the by-name getters and the record mappers), such as name => name.Replace("_", "") so that unit_price matches UnitPrice. Matching then works as usual: exactly, then ignoring case and surrounding whitespace. CsvReader.Headers keeps the names as they are. Defaults to null.

Property value

Func<string, string>?

View source

OnRecordError

Added in v34.0.0
public Func<CsvRecordError, bool>? OnRecordError { get; init; }

Called when a record of a CsvRecordAttribute type cannot be mapped, such as a field that does not parse, by CsvReader.GetRecords, CsvReader.GetRecordsAsync, CsvReader.EnumerateRecords and CsvReader.ParallelEnumerate<T>(int) (where it can be called on several threads at once). Return true to skip the record and go on, or false to throw the exception. Defaults to null: the exception ends the enumeration.

Property value

View source

PoolStrings

Added in v34.0.0
public bool PoolStrings { get; init; }

Whether repeated string values are de-duplicated per column, so reading many records with the same values allocates each distinct string once. Columns whose values rarely repeat switch pooling off by themselves. Defaults to false: a lookup costs about as much as creating a short string, so pooling pays off when the strings are kept (many records held in memory, where it saves memory and garbage collection work), not when each string is used and dropped.

Property value

bool

View source

Quote

Added in v34.0.0
public char Quote { get; init; }

The quote character of CsvMode.Rfc4180 mode. Defaults to ". Must be an ASCII character.

Property value

char

View source

Separator

Added in v34.0.0
public char? Separator { get; init; }

The field separator. When null (the default), the separator is detected from the first lines of the input, choosing among ,;\t and |. Must be an ASCII character. Not used in CsvMode.FixedWidth mode.

Property value

char?

View source

SkipEmptyLines

Added in v34.0.0
public bool SkipEmptyLines { get; init; }

Whether completely empty lines are skipped. Defaults to true.

Property value

bool

View source

TrackLineNumbers

Added in v34.0.0
public bool TrackLineNumbers { get; init; }

Whether the reader counts the lines of the input, for CsvReader.LineNumber and the line numbers of errors: every line, including those inside quoted fields, empty lines and comments. Defaults to false, since counting looks at every record (reading is a little slower).

Property value

bool

View source

TrimInsideQuotes

Added in v34.0.0
public bool TrimInsideQuotes { get; init; }

Whether whitespace at the start and end of the value of a quoted field is removed too (" a " reads as a). Defaults to false. Independent of TrimWhitespace, which trims outside the quotes. CsvMode.Rfc4180 mode only.

Property value

bool

View source

TrimWhitespace

Added in v34.0.0
public bool? TrimWhitespace { get; init; }

Whether spaces and tabs around field values are removed. Whitespace inside quotes is kept, a quote that follows leading whitespace still opens a quoted field, and escaped whitespace is kept. Defaults to null: values are trimmed in CsvMode.FixedWidth mode (where columns are padded) and not otherwise.

Property value

bool?

View source

WhitespaceCharacters

Added in v34.0.0
public string WhitespaceCharacters { get; init; }

The characters that TrimWhitespace and TrimInsideQuotes remove, and that may come before the opening quote of a field when trimming. Defaults to space and tab. Must be ASCII characters other than CR, LF, the quote and the escape character; the separator still separates fields.

Property value

string

View source

Methods

For<T>()

Added in v34.0.0static
public static CsvReaderOptions For<T>()

The options for the files of T: the defaults with the settings of the type's CsvOptionsAttribute, or Default when it has none. Change the rest with a with expression, such as CsvReaderOptions.For<Order>() with { PoolStrings = true }.

Type parameters

T
A CsvRecordAttribute type, or a type mapped by a CsvRecordMapAttribute.

Returns

CsvReaderOptions
The options, the same instance on every call.

Exceptions

CultureNotFoundException
The attribute names a culture that does not exist (or the app runs in globalization invariant mode).

View source