CsvReaderOptions Class

Options that control how CsvReader and Utf8CsvReader read CSV data.

public sealed class CsvReaderOptions : IEquatable<CsvReaderOptions>

Remarks

The options are immutable. Use a with expression to create a copy with other values.

Implements

Constructors

NameDescription
CsvReaderOptions()

CsvReaderOptions()

CsvReaderOptions.cs
public CsvReaderOptions()

No documentation is available.

Properties

NameDescription
BadDataHandlerGets the method called for malformed data, which decides whether the reader throws, accepts the data, or skips the record. Default is null.
BufferSizeGets the initial size, in characters or in bytes, of the buffer of the reader. Default is 16384.
ColumnNameComparerGets the comparer used to find a column by name. Default is StringComparer.Ordinal.
CommentCharacterGets the character that starts a comment line, or null when the data has no comments. Default is null.
DefaultGets the default options: comma separator, double quote, a header row.
FormatProviderGets the format provider used to parse values. Default is null, which means CultureInfo.InvariantCulture.
HasHeaderRowGets a value indicating whether the first row holds the names of the columns. Default is true.
MaxRowLengthGets the maximum length, in characters or in bytes, of a row read from a TextReader or a Stream. Default is 16777216.
PoolStringsGets a value indicating whether the strings of short values are reused instead of allocated again. Default is false.
QuoteGets the character that quotes values. Default is ". When null, quote characters are part of the values.
SeparatorGets the character that separates the values of a row. Default is ,.
SkipEmptyLinesGets a value indicating whether empty lines are skipped. Default is false: an empty line is a record without columns.
StrictGets a value indicating whether invalid data throws a CsvException. Default is false.
TrimGets which spaces are removed from the values. Default is CsvTrimOptions.None.
ValidateColumnCountGets a value indicating whether every record must have the same number of columns as the header row, or as the first record when there is no header row.

BadDataHandler

CsvReaderOptions.cs

Gets the method called for malformed data, which decides whether the reader throws, accepts the data, or skips the record. Default is null.

public CsvBadDataHandler? BadDataHandler { get; init; }

Property value

CsvBadDataHandler?

Remarks

The handler is called for the errors that CsvReaderOptions.Strict reports (see CsvBadDataKind), whatever the value of CsvReaderOptions.Strict, and for a record with an unexpected number of columns when CsvReaderOptions.ValidateColumnCount is true. When it is null, CsvReaderOptions.Strict decides for the quoting errors: the reader throws in strict mode and accepts the data otherwise. A record with an unexpected number of columns throws whenever CsvReaderOptions.ValidateColumnCount is true, whatever the value of CsvReaderOptions.Strict.

The handler is called in the order of the data, once every record before the malformed one has been read, and before the reader returns the malformed record. A record can have several errors, reported one by one until the handler returns CsvBadDataAction.Skip. The handler must not call the methods of the reader. It may throw its own exception, which ends the reading.

The handler does not change how the data is read when it is valid, and it is not called for a row longer than CsvReaderOptions.MaxRowLength, which always throws. The quoting errors of such a row can be reported before the reader finds that the row is too long.

Example

var errors = new List<string>();
var options = new CsvReaderOptions
{
    BadDataHandler = context =>
    {
        errors.Add($"line {context.LineNumber}, column {context.ColumnName}: {context.Message}");
        return CsvBadDataAction.Skip; // or Accept to read the value as is, or Throw
    },
};

using var reader = CsvReader.CreateFromString("Name,Comment\nAlice,ok\nBob,\"quoted\" text\nCharlie,ok", options);
var names = new List<string>();
while (reader.Read())
{
    names.Add(reader.Current["Name"].ToString());
}

Assert.Equal(["Alice", "Charlie"], names);
Assert.Equal(["line 3, column Comment: A quoted value is followed by characters that are not a separator."], errors);

Gets the initial size, in characters or in bytes, of the buffer of the reader. Default is 16384.

public int BufferSize { get; init; }

Property value

int

Remarks

The buffer grows when a row does not fit, up to CsvReaderOptions.MaxRowLength.

ColumnNameComparer

CsvReaderOptions.cs

Gets the comparer used to find a column by name. Default is StringComparer.Ordinal.

public IEqualityComparer<string> ColumnNameComparer { get; init; }

CommentCharacter

CsvReaderOptions.cs

Gets the character that starts a comment line, or null when the data has no comments. Default is null.

public char? CommentCharacter { get; init; }

Property value

char?

Remarks

A line is a comment when its first character is this character. Comment lines are skipped, including before the header row.

Gets the default options: comma separator, double quote, a header row.

public static CsvReaderOptions Default { get; }

Property value

CsvReaderOptions

FormatProvider

CsvReaderOptions.cs

Gets the format provider used to parse values. Default is null, which means CultureInfo.InvariantCulture.

public IFormatProvider? FormatProvider { get; init; }

Property value

IFormatProvider?

Gets a value indicating whether the first row holds the names of the columns. Default is true.

public bool HasHeaderRow { get; init; }

Property value

bool

Remarks

The header row is not returned as a record; the names are available from the Header property of the reader once the first record is read.

Gets the maximum length, in characters or in bytes, of a row read from a TextReader or a Stream. Default is 16777216.

public int MaxRowLength { get; init; }

Property value

int

Remarks

A longer row throws a CsvException. This limits the memory used by malformed data, such as a quote that is never closed.

Gets a value indicating whether the strings of short values are reused instead of allocated again. Default is false.

public bool PoolStrings { get; init; }

Property value

bool

Remarks

Each reader keeps its own pool of strings of at most 32 characters. This reduces the allocations when the data repeats the same values, such as categories or flags.

Gets the character that quotes values. Default is ". When null, quote characters are part of the values.

public char? Quote { get; init; }

Property value

char?

Remarks

Utf8CsvReader requires an ASCII quote character.

Gets the character that separates the values of a row. Default is ,.

public char? Separator { get; init; }

Property value

char?

Remarks

When null, the separator is detected from the first line of the data: the most frequent character among ,, ;, tab and | outside quoted values, or , when the line contains none of them. Utf8CsvReader requires an ASCII separator.

SkipEmptyLines

CsvReaderOptions.cs

Gets a value indicating whether empty lines are skipped. Default is false: an empty line is a record without columns.

public bool SkipEmptyLines { get; init; }

Property value

bool

Gets a value indicating whether invalid data throws a CsvException. Default is false.

public bool Strict { get; init; }

Property value

bool

Remarks

In strict mode, a quote character inside a value that is not quoted, a character other than a separator or a line terminator after the closing quote of a value, and a quoted value that does not end before the end of the data are errors. Otherwise, a quote character inside a value that is not quoted is part of the value, the characters after a closing quote are part of the value, and an unterminated quoted value ends at the end of the data. When CsvReaderOptions.BadDataHandler is set, it decides what happens to these errors, and this property is not used.

Gets which spaces are removed from the values. Default is CsvTrimOptions.None.

public CsvTrimOptions Trim { get; init; }

Property value

CsvTrimOptions

ValidateColumnCount

CsvReaderOptions.cs

Gets a value indicating whether every record must have the same number of columns as the header row, or as the first record when there is no header row.

public bool ValidateColumnCount { get; init; }

Property value

bool

Remarks

A record with a different number of columns throws a CsvException, unless CsvReaderOptions.BadDataHandler decides otherwise.

Methods

NameDescription
Equals(CsvReaderOptions?)
Equals(object?)
GetHashCode()
ToString()

Equals overloads

Equals(CsvReaderOptions?)

public bool Equals(CsvReaderOptions? other)
Parameters
other CsvReaderOptions?
Returns

bool

No documentation is available.

Equals(object?)

public override bool Equals(object? obj)
Parameters
obj object?
Returns

bool

No documentation is available.

GetHashCode()

public override int GetHashCode()

Returns

int

No documentation is available.

ToString()

public override string ToString()

Returns

string

No documentation is available.

Operators

NameDescription
operator ==(CsvReaderOptions?, CsvReaderOptions?)
operator !=(CsvReaderOptions?, CsvReaderOptions?)

operator ==(CsvReaderOptions?, CsvReaderOptions?)

public static bool operator ==(CsvReaderOptions? left, CsvReaderOptions? right)

Parameters

left CsvReaderOptions?
right CsvReaderOptions?

Returns

bool

No documentation is available.

operator !=(CsvReaderOptions?, CsvReaderOptions?)

public static bool operator !=(CsvReaderOptions? left, CsvReaderOptions? right)

Parameters

left CsvReaderOptions?
right CsvReaderOptions?

Returns

bool

No documentation is available.

Constants

NameDescription
DefaultQuoteThe default quote character (double quote).
DefaultSeparatorThe default separator character (comma).

DefaultQuote

The default quote character (double quote).

public const char DefaultQuote = '"'

DefaultSeparator

The default separator character (comma).

public const char DefaultSeparator = ','