net10.0, netstandard2.1, netstandard2.0

public sealed class CsvDynamicRecord : IDynamicMetaObjectProvider, IReadOnlyDictionary<string, string?>, IEnumerable<KeyValuePair<string, string?>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, string?>>

net47

public sealed class CsvDynamicRecord : IDynamicMetaObjectProvider, IReadOnlyDictionary<string, string?>, IReadOnlyCollection<KeyValuePair<string, string?>>, IEnumerable<KeyValuePair<string, string?>>, IEnumerable
Namespace
CsvHelper
Assembly
CsvHelper.dll
Builds
net10.0 netstandard2.1 netstandard2.0 net47

Remarks

A record holds the bytes of its fields, not strings: a value becomes a string when it is read, each time it is read, so the fields you never read cost nothing more. The values are strings (null for one of the reader's CsvReaderOptions.NullValues); Parse, such as record.Parse<int>("Count"), parses them as the reader's CsvReader.Parse<T>(int) does.

The records of a reader share its schema, so record.Name is bound once to the column's index, and later reads of it, on any record of the same header, go straight to the field. A column takes precedence over a member of this class with the same name: with a column FieldCount, record.FieldCount is its value.

Records keep their bytes in blocks shared with the records read after them (or, for input in a byte array, in the input itself), so keeping one record keeps its block in memory; Detach makes a copy that holds only its own bytes. Records do not change, so they can be used on any thread.

Properties

FieldCount

Added in v34.0.0
public int FieldCount { get; }

The number of fields of the record.

Property value

int

View source

Names

Added in v34.0.0
public IReadOnlyList<string> Names { get; }

The names of the columns: the header names, or Field1, Field2 and so on for each field of the record when the input has no header.

Property value

View source

Indexers

this[int]

Added in v34.0.0
public string? this[int index] { get; }

Gets field index as a string, or null when it is one of the reader's CsvReaderOptions.NullValues. A field past the end of the record reads as empty, unless the reader's CsvReaderOptions.MissingField is CsvMissingFieldHandling.Throw.

Parameters

indexint
The 0-based index of the field.

Property value

string?

Exceptions

ArgumentOutOfRangeException
index is negative.
IndexOutOfRangeException
The record has no such field and missing fields throw.

View source

Methods

Detach()

Added in v34.0.0
public CsvDynamicRecord Detach()

Makes a copy of the record that holds only its own bytes, for a record that you keep while dropping the others: a record read from a stream or a file keeps its whole block of records in memory, and one read from a byte array keeps the array.

Returns

CsvDynamicRecord
A record with the same values.

View source

Parse<T>(string)

Added in v34.0.0

net10.0

public T Parse<T>(string name) where T : ISpanParsable<T>

Parses the field of the column called name as T, with the reader's format provider and its CsvReaderOptions.TrueValues and CsvReaderOptions.FalseValues, as CsvReader.Parse<T>(int) does.

Type parameters

T
The type to parse the field as.

Parameters

namestring
The name of the column, matched as by CsvReader.TryGetFieldIndex(string, out int).

Returns

T
The parsed value.

Exceptions

KeyNotFoundException
No column has that name.
FormatException
The field is not a valid T.
OverflowException
The field is outside the range of T.
NotSupportedException
On .NET Framework and .NET Standard, T is not one of the built-in types of CsvReader.Parse<T>(int).
IndexOutOfRangeException
The record has no field in that column and missing fields throw.

netstandard2.1, netstandard2.0, net47

public T Parse<T>(string name)

Parses the field of the column called name as T, with the reader's format provider and its CsvReaderOptions.TrueValues and CsvReaderOptions.FalseValues, as CsvReader.Parse<T>(int) does.

Type parameters

T
The type to parse the field as.

Parameters

namestring
The name of the column, matched as by CsvReader.TryGetFieldIndex(string, out int).

Returns

T
The parsed value.

Exceptions

KeyNotFoundException
No column has that name.
FormatException
The field is not a valid T.
OverflowException
The field is outside the range of T.
NotSupportedException
On .NET Framework and .NET Standard, T is not one of the built-in types of CsvReader.Parse<T>(int).
IndexOutOfRangeException
The record has no field in that column and missing fields throw.

View source

ToDictionary()

Added in v34.0.0
public Dictionary<string, string?> ToDictionary()

Copies the values into a new dictionary, which you can change, keyed by column name.

Returns

Dictionary<string, string?>
The values by name, in column order.

View source

ToString()

Added in v34.0.0
public override string ToString()

The names and values of the record, such as Name: Ann, Count: 3, for debugging.

Returns

string
The names and values.

View source

public bool TryGetValue(string name, out string? value)

Gets the field of the column called name, if there is one. See this[int].

Parameters

namestring
The name of the column, matched as by CsvReader.TryGetFieldIndex(string, out int).
valuestring?
The value of the field, or null when there is no such column.

Returns

bool
Whether the record has the column.

Exceptions

IndexOutOfRangeException
The record has no field in that column and missing fields throw.

View source