Utf8CsvReader Class

Reads UTF-8 encoded CSV data, one record at a time, without decoding it to UTF-16.

public sealed class Utf8CsvReader : IAsyncDisposable, IDisposable

Remarks

The values of the current record are slices of the internal buffer of the reader: the Utf8CsvRecord returned by Utf8CsvReader.Current and the spans of its values are valid until the next call to Utf8CsvReader.Read or Utf8CsvReader.ReadAsync. Call Utf8CsvField.ToString to keep a value.

The separator, the quote and the comment characters must be ASCII characters. A UTF-8 byte order mark at the start of the data is skipped.

This type is not thread-safe.

Example

using var stream = new MemoryStream("Name,Age\nAlice,30\nBob,25"u8.ToArray());
using var reader = Utf8CsvReader.Create(stream);

var totalAge = 0;
var names = new List<string>();
foreach (var record in reader)
{
    names.Add(record["Name"].ToString());
    totalAge += record["Age"].Parse<int>(); // Parses the UTF-8 bytes, without decoding them
}

Assert.Equal(["Alice", "Bob"], names);
Assert.Equal(55, totalAge);
Implements

Nested types

NameDescription
Utf8CsvReader.EnumeratorEnumerates the records of a Utf8CsvReader.

Properties

NameDescription
CurrentGets the current record.
HeaderGets the names of the columns, read from the header row, or null when the data has no header row or when no record has been read yet.
LineNumberGets the 1-based line number where the current record starts. Every line terminator counts, including those inside quoted values.
PositionGets the 0-based position, in bytes from the start of the data, where the current record starts.
RowIndexGets the 0-based index of the current record. The header row and the comment lines are not counted.
SeparatorGets the separator character, which is the detected one when CsvReaderOptions.Separator is null.

Gets the current record.

public Utf8CsvRecord Current { get; }

Gets the 1-based line number where the current record starts. Every line terminator counts, including those inside quoted values.

public long LineNumber { get; }

Property value

long

Gets the 0-based position, in bytes from the start of the data, where the current record starts.

public long Position { get; }

Property value

long

Gets the 0-based index of the current record. The header row and the comment lines are not counted.

public long RowIndex { get; }

Property value

long

Gets the separator character, which is the detected one when CsvReaderOptions.Separator is null.

public char Separator { get; }

Property value

char

Remarks

The separator is detected when the first record is read.

Methods

NameDescription
Create(Stream, CsvReaderOptions?, bool)Creates a reader that reads UTF-8 CSV data from a Stream.
Create(ReadOnlyMemory<byte>, CsvReaderOptions?)Creates a reader that reads UTF-8 CSV data from memory.
CreateFromFile(string, CsvReaderOptions?)Creates a reader that reads UTF-8 CSV data from a file.
Dispose()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
DisposeAsync()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
GetEnumerator()Gets an enumerator that reads the records one by one, for use in a foreach loop.
Read()Reads the next record.
ReadAsync(CancellationToken)Reads the next record asynchronously.

Create overloads

Create(Stream, CsvReaderOptions?, bool)

Utf8CsvReader.cs

Creates a reader that reads UTF-8 CSV data from a Stream.

public static Utf8CsvReader Create(Stream stream, CsvReaderOptions? options = null, bool leaveOpen = false)
Parameters
stream Stream
The stream to read the data from.
options CsvReaderOptions?
The options, or null to use CsvReaderOptions.Default.
leaveOpen bool
true to leave stream open when the Utf8CsvReader is disposed.
Returns

Utf8CsvReader The new reader.

Create(ReadOnlyMemory<byte>, CsvReaderOptions?)

Utf8CsvReader.cs

Creates a reader that reads UTF-8 CSV data from memory.

public static Utf8CsvReader Create(ReadOnlyMemory<byte> utf8Csv, CsvReaderOptions? options = null)
Parameters
utf8Csv ReadOnlyMemory<byte>
The UTF-8 encoded CSV data.
options CsvReaderOptions?
The options, or null to use CsvReaderOptions.Default.
Returns

Utf8CsvReader The new reader.

Remarks

The data is not copied: the values are slices of utf8Csv.

CreateFromFile(string, CsvReaderOptions?)

Utf8CsvReader.cs

Creates a reader that reads UTF-8 CSV data from a file.

public static Utf8CsvReader CreateFromFile(string path, CsvReaderOptions? options = null)

Parameters

path string
The path of the file.
options CsvReaderOptions?
The options, or null to use CsvReaderOptions.Default.

Returns

Utf8CsvReader The new reader. Dispose it to close the file.

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

DisposeAsync()

Utf8CsvReader.cs

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous dispose operation.

GetEnumerator()

Utf8CsvReader.cs

Gets an enumerator that reads the records one by one, for use in a foreach loop.

public Utf8CsvReader.Enumerator GetEnumerator()

Returns

Utf8CsvReader.Enumerator An enumerator over the records.

Reads the next record.

public bool Read()

Returns

bool true when a record was read; false at the end of the data.

Exceptions

CsvException
The data is invalid.

ReadAsync(CancellationToken)

Utf8CsvReader.cs

Reads the next record asynchronously.

public ValueTask<bool> ReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

ValueTask<bool> true when a record was read; false at the end of the data.

Exceptions

CsvException
The data is invalid.