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
| Name | Description |
|---|---|
| CsvReaderOptions() |
CsvReaderOptions()
CsvReaderOptions.cspublic CsvReaderOptions()No documentation is available.
Properties
| Name | Description |
|---|---|
| BadDataHandler | Gets the method called for malformed data, which decides whether the reader throws, accepts the data, or skips the record. Default is null. |
| BufferSize | Gets the initial size, in characters or in bytes, of the buffer of the reader. Default is 16384. |
| ColumnNameComparer | Gets the comparer used to find a column by name. Default is StringComparer.Ordinal. |
| CommentCharacter | Gets the character that starts a comment line, or null when the data has no comments. Default is null. |
| Default | Gets the default options: comma separator, double quote, a header row. |
| FormatProvider | Gets the format provider used to parse values. Default is null, which means CultureInfo.InvariantCulture. |
| HasHeaderRow | Gets a value indicating whether the first row holds the names of the columns. Default is true. |
| MaxRowLength | Gets the maximum length, in characters or in bytes, of a row read from a TextReader or a Stream. Default is 16777216. |
| PoolStrings | Gets a value indicating whether the strings of short values are reused instead of allocated again. Default is false. |
| Quote | Gets the character that quotes values. Default is ". When null, quote characters are part of the values. |
| Separator | Gets the character that separates the values of a row. Default is ,. |
| SkipEmptyLines | Gets a value indicating whether empty lines are skipped. Default is false: an empty line is a record without columns. |
| Strict | Gets a value indicating whether invalid data throws a CsvException. Default is false. |
| Trim | Gets which spaces are removed from the values. Default is CsvTrimOptions.None. |
| ValidateColumnCount | 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. |
BadDataHandler
CsvReaderOptions.csGets 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
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);BufferSize
CsvReaderOptions.csGets the initial size, in characters or in bytes, of the buffer of the reader. Default is 16384.
public int BufferSize { get; init; }ColumnNameComparer
CsvReaderOptions.csGets the comparer used to find a column by name. Default is StringComparer.Ordinal.
public IEqualityComparer<string> ColumnNameComparer { get; init; }Property value
CommentCharacter
CsvReaderOptions.csDefault
CsvReaderOptions.csGets the default options: comma separator, double quote, a header row.
public static CsvReaderOptions Default { get; }Property value
FormatProvider
CsvReaderOptions.csGets the format provider used to parse values. Default is null, which means CultureInfo.InvariantCulture.
public IFormatProvider? FormatProvider { get; init; }Property value
HasHeaderRow
CsvReaderOptions.csMaxRowLength
CsvReaderOptions.csGets 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
Remarks
A longer row throws a CsvException. This limits the memory used by malformed data, such as a quote that is never closed.
PoolStrings
CsvReaderOptions.csGets 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
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.
Quote
CsvReaderOptions.csGets the character that quotes values. Default is ". When null, quote characters are part of the values.
public char? Quote { get; init; }Separator
CsvReaderOptions.csGets the character that separates the values of a row. Default is ,.
public char? Separator { get; init; }Property value
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.csStrict
CsvReaderOptions.csGets a value indicating whether invalid data throws a CsvException. Default is false.
public bool Strict { get; init; }Property value
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
ValidateColumnCount
CsvReaderOptions.csGets 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
Remarks
A record with a different number of columns throws a CsvException, unless CsvReaderOptions.BadDataHandler decides otherwise.
Methods
| Name | Description |
|---|---|
| Equals(CsvReaderOptions?) | |
| Equals(object?) | |
| GetHashCode() | |
| ToString() |
Equals overloads
Equals(CsvReaderOptions?)
public bool Equals(CsvReaderOptions? other)No documentation is available.
Equals(object?)
GetHashCode()
ToString()
Operators
| Name | Description |
|---|---|
| operator ==(CsvReaderOptions?, CsvReaderOptions?) | |
| operator !=(CsvReaderOptions?, CsvReaderOptions?) |
operator ==(CsvReaderOptions?, CsvReaderOptions?)
public static bool operator ==(CsvReaderOptions? left, CsvReaderOptions? right)No documentation is available.
operator !=(CsvReaderOptions?, CsvReaderOptions?)
public static bool operator !=(CsvReaderOptions? left, CsvReaderOptions? right)No documentation is available.
Constants
| Name | Description |
|---|---|
| DefaultQuote | The default quote character (double quote). |
| DefaultSeparator | The 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 = ','