CsvReaderOptions()
public CsvReaderOptions()Initializes a new instance of CsvReaderOptions.
Record in CsvHelper
Options for CsvReader. The defaults adapt to most real-world files.
public sealed record CsvReaderOptionsCsvHelperCsvHelper.dllpublic CsvReaderOptions()Initializes a new instance of CsvReaderOptions.
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.
public int BufferSize { get; init; }The size in bytes of the read buffer. Defaults to 64 KiB. Grows automatically for long records.
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.
public static CsvReaderOptions Default { get; }The default options.
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.
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.
public char Escape { get; init; }The escape character of CsvMode.Escape mode. Defaults to \. Must be an ASCII character other than space and tab.
public IReadOnlyList<string>? FalseValues { get; init; }The values that Parse<bool> (CsvReader.Parse<T>(int)) reads as false, instead of false, no, n, f and 0; matched ignoring ASCII case and surrounding spaces and tabs. Defaults to null: the built-in values.
public CsvFieldCountHandling FieldCountMismatch { get; init; }What to do when records have different field counts. Defaults to CsvFieldCountHandling.Allow.
public IReadOnlyList<int>? FieldWidths { get; init; }The widths of the columns in CsvMode.FixedWidth mode, in characters (UTF-16 code units, as string.Length counts them), from the start of the line. Required in that mode; every width must be positive. Text after the last column is not part of any field (it is still part of CsvReader.RawRecord), and a short line has empty fields at its end.
public IFormatProvider FormatProvider { get; init; }The format provider used for parsing numbers and dates. Defaults to CultureInfo.InvariantCulture.
public bool HasHeader { get; init; }Whether the first record is a header row. Defaults to true.
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.
public CsvMalformedDataHandling MalformedData { get; init; }What to do with malformed quoting. Defaults to CsvMalformedDataHandling.Recover.
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.
public CsvMissingFieldHandling MissingField { get; init; }What happens when a missing field is requested. Defaults to CsvMissingFieldHandling.ReturnEmpty.
public CsvMode Mode { get; init; }How fields are delimited and escaped. Defaults to CsvMode.Rfc4180; see CsvMode for the others.
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.
public IReadOnlyList<string>? NullValues { get; init; }Field values that mean null, such as NULL or \N, matched exactly (after trimming): for CsvReader.IsNull, CsvReader.GetStringOrNull(int), ParseOrNull, and the Nullable<T> and nullable string members of CsvRecordAttribute types. An empty field is null for those too, except for strings, which read it as "" unless "" is listed. Defaults to null.
public Action<CsvMalformedRecord>? OnMalformedRecord { get; init; }Called for every malformed record found (bad quoting, or a field count mismatch when FieldCountMismatch is not CsvFieldCountHandling.Allow), whether it was recovered or skipped. Setting it enables malformed quote detection, which is a little slower on heavily quoted data. Not called for records that cause an exception.
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.
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.
public char Quote { get; init; }The quote character of CsvMode.Rfc4180 mode. Defaults to ". Must be an ASCII character.
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.
public CsvShouldSkipRecord? ShouldSkipRecord { get; init; }Called for every data record (not the header) before CsvReader.Read returns it; returning true skips the record, so CsvReader.GetRecords never sees it. The record's CsvRow.RowNumber is the number it gets when it is not skipped. Defaults to null.
public bool SkipEmptyLines { get; init; }Whether completely empty lines are skipped. Defaults to true.
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).
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.
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.
public IReadOnlyList<string>? TrueValues { get; init; }The values that Parse<bool> (CsvReader.Parse<T>(int)) reads as true, instead of true, yes, y, t and 1; matched ignoring ASCII case and surrounding spaces and tabs. Defaults to null: the built-in values.
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.
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 }.
T