Utf8CsvReader Class
Reads UTF-8 encoded CSV data, one record at a time, without decoding it to UTF-16.
public sealed class Utf8CsvReader : IAsyncDisposable, IDisposableRemarks
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
| Name | Description |
|---|---|
| Utf8CsvReader.Enumerator | Enumerates the records of a Utf8CsvReader. |
Properties
| Name | Description |
|---|---|
| Current | Gets the current record. |
| Header | Gets 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. |
| LineNumber | Gets the 1-based line number where the current record starts. Every line terminator counts, including those inside quoted values. |
| Position | Gets the 0-based position, in bytes from the start of the data, where the current record starts. |
| RowIndex | Gets the 0-based index of the current record. The header row and the comment lines are not counted. |
| Separator | Gets the separator character, which is the detected one when CsvReaderOptions.Separator is null. |
Current
Utf8CsvReader.csGets the current record.
public Utf8CsvRecord Current { get; }Property value
Exceptions
InvalidOperationExceptionUtf8CsvReader.ReadorUtf8CsvReader.ReadAsynchas not returnedtrue.
Header
Utf8CsvReader.csGets 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.
public CsvHeader? Header { get; }Property value
LineNumber
Utf8CsvReader.csPosition
Utf8CsvReader.csRowIndex
Utf8CsvReader.csSeparator
Utf8CsvReader.csGets the separator character, which is the detected one when CsvReaderOptions.Separator is null.
public char Separator { get; }Methods
| Name | Description |
|---|---|
| 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.csCreates a reader that reads UTF-8 CSV data from a Stream.
public static Utf8CsvReader Create(Stream stream, CsvReaderOptions? options = null, bool leaveOpen = false)Parameters
streamStream- The stream to read the data from.
optionsCsvReaderOptions?- The options, or
nullto useCsvReaderOptions.Default. leaveOpenbooltrueto leavestreamopen when theUtf8CsvReaderis disposed.
Returns
Utf8CsvReader The new reader.
Create(ReadOnlyMemory<byte>, CsvReaderOptions?)
Utf8CsvReader.csCreates a reader that reads UTF-8 CSV data from memory.
public static Utf8CsvReader Create(ReadOnlyMemory<byte> utf8Csv, CsvReaderOptions? options = null)Parameters
utf8CsvReadOnlyMemory<byte>- The UTF-8 encoded CSV data.
optionsCsvReaderOptions?- The options, or
nullto useCsvReaderOptions.Default.
Returns
Utf8CsvReader The new reader.
Remarks
The data is not copied: the values are slices of utf8Csv.
CreateFromFile(string, CsvReaderOptions?)
Utf8CsvReader.csCreates a reader that reads UTF-8 CSV data from a file.
public static Utf8CsvReader CreateFromFile(string path, CsvReaderOptions? options = null)Parameters
pathstring- The path of the file.
optionsCsvReaderOptions?- The options, or
nullto useCsvReaderOptions.Default.
Returns
Utf8CsvReader The new reader. Dispose it to close the file.
Dispose()
Utf8CsvReader.csPerforms application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()DisposeAsync()
Utf8CsvReader.csGetEnumerator()
Utf8CsvReader.csGets 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.
Read()
Utf8CsvReader.csReads 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.csReads the next record asynchronously.
public ValueTask<bool> ReadAsync(CancellationToken cancellationToken = default)Parameters
cancellationTokenCancellationToken- 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.