CsvSerializer Class

Reads CSV data as a list of objects, and writes objects as CSV data. Each record is an object; each column is a property or a field.

public static class CsvSerializer

Remarks

The overloads that take a CsvSerializerOptions create the metadata of the type with reflection. They are not compatible with trimming and Native AOT: use the overloads that take a CsvTypeInfo<T> from a generated CsvSerializerContext instead.

The overloads that take a Stream read and write UTF-8 data without converting it to UTF-16.

Example

var people = new List<Person>
{
    new() { Name = "Alice", Age = 30, BirthDate = new DateOnly(1995, 4, 12) },
    new() { Name = "Bob, Jr.", Age = 25 },
};

var csv = CsvSerializer.Serialize(people);
Assert.Equal("Name,Age,BirthDate\r\nAlice,30,1995-04-12\r\n\"Bob, Jr.\",25,\r\n", csv);

var result = CsvSerializer.Deserialize<Person>(csv);
Assert.Equal("Bob, Jr.", result[1].Name);
Assert.Null(result[1].BirthDate); // An empty value is null for a nullable type

Properties

NameDescription
IsReflectionEnabledByDefaultGets a value indicating whether the overloads that take a CsvSerializerOptions can create metadata with reflection.

IsReflectionEnabledByDefault

CsvSerializer.cs

Gets a value indicating whether the overloads that take a CsvSerializerOptions can create metadata with reflection.

public static bool IsReflectionEnabledByDefault { get; }

Property value

bool

Remarks

This is a feature switch, disabled when publishing with Native AOT. Set the MeziantouFrameworkCsvIsReflectionEnabledByDefault MSBuild property to override it. When disabled, the trimmer removes the reflection-based code.

Methods

NameDescription
Deserialize<T>(Stream, CsvSerializerOptions?)Reads the records of UTF-8 CSV data from a Stream.
Deserialize<T>(Stream, CsvTypeInfo<T>)Reads the records of UTF-8 CSV data from a Stream.
Deserialize<T>(TextReader, CsvSerializerOptions?)Reads the records of CSV data from a TextReader.
Deserialize<T>(TextReader, CsvTypeInfo<T>)Reads the records of CSV data from a TextReader.
Deserialize<T>(ReadOnlyMemory<byte>, CsvSerializerOptions?)Reads the records of UTF-8 CSV data.
Deserialize<T>(ReadOnlyMemory<byte>, CsvTypeInfo<T>)Reads the records of UTF-8 CSV data.
Deserialize<T>(string, CsvSerializerOptions?)Reads the records of CSV data.
Deserialize<T>(string, CsvTypeInfo<T>)Reads the records of CSV data.
DeserializeAsync<T>(Stream, CsvSerializerOptions?, CancellationToken)Reads the records of UTF-8 CSV data from a Stream asynchronously.
DeserializeAsync<T>(Stream, CsvTypeInfo<T>, CancellationToken)Reads the records of UTF-8 CSV data from a Stream asynchronously.
DeserializeAsync<T>(TextReader, CsvSerializerOptions?, CancellationToken)Reads the records of CSV data from a TextReader asynchronously.
DeserializeAsync<T>(TextReader, CsvTypeInfo<T>, CancellationToken)Reads the records of CSV data from a TextReader asynchronously.
DeserializeAsyncEnumerable<T>(Stream, CsvSerializerOptions?, CancellationToken)Reads the records of UTF-8 CSV data from a Stream one by one, asynchronously, as they are enumerated.
DeserializeAsyncEnumerable<T>(Stream, CsvTypeInfo<T>, CancellationToken)Reads the records of UTF-8 CSV data from a Stream one by one, asynchronously, as they are enumerated.
DeserializeAsyncEnumerable<T>(TextReader, CsvSerializerOptions?, CancellationToken)Reads the records of CSV data from a TextReader one by one, asynchronously, as they are enumerated.
DeserializeAsyncEnumerable<T>(TextReader, CsvTypeInfo<T>, CancellationToken)Reads the records of CSV data from a TextReader one by one, asynchronously, as they are enumerated.
DeserializeEnumerable<T>(Stream, CsvSerializerOptions?)Reads the records of UTF-8 CSV data from a Stream one by one, as they are enumerated.
DeserializeEnumerable<T>(Stream, CsvTypeInfo<T>)Reads the records of UTF-8 CSV data from a Stream one by one, as they are enumerated.
DeserializeEnumerable<T>(TextReader, CsvSerializerOptions?)Reads the records of CSV data from a TextReader one by one, as they are enumerated.
DeserializeEnumerable<T>(TextReader, CsvTypeInfo<T>)Reads the records of CSV data from a TextReader one by one, as they are enumerated.
Serialize<T>(IBufferWriter<byte>, IEnumerable<T>, CsvSerializerOptions?)Writes records as UTF-8 CSV data to an IBufferWriter<T>.
Serialize<T>(IBufferWriter<byte>, IEnumerable<T>, CsvTypeInfo<T>)Writes records as UTF-8 CSV data to an IBufferWriter<T>.
Serialize<T>(IEnumerable<T>, CsvSerializerOptions?)Writes records as CSV text.
Serialize<T>(IEnumerable<T>, CsvTypeInfo<T>)Writes records as CSV text.
Serialize<T>(Stream, IEnumerable<T>, CsvSerializerOptions?)Writes records as UTF-8 CSV data to a Stream.
Serialize<T>(Stream, IEnumerable<T>, CsvTypeInfo<T>)Writes records as UTF-8 CSV data to a Stream.
Serialize<T>(TextWriter, IEnumerable<T>, CsvSerializerOptions?)Writes records as CSV text to a TextWriter.
Serialize<T>(TextWriter, IEnumerable<T>, CsvTypeInfo<T>)Writes records as CSV text to a TextWriter.
SerializeAsync<T>(Stream, IAsyncEnumerable<T>, CsvSerializerOptions?, CancellationToken)Writes records as UTF-8 CSV data to a Stream asynchronously, as they are produced.
SerializeAsync<T>(Stream, IAsyncEnumerable<T>, CsvTypeInfo<T>, CancellationToken)Writes records as UTF-8 CSV data to a Stream asynchronously, as they are produced.
SerializeAsync<T>(Stream, IEnumerable<T>, CsvSerializerOptions?, CancellationToken)Writes records as UTF-8 CSV data to a Stream asynchronously.
SerializeAsync<T>(Stream, IEnumerable<T>, CsvTypeInfo<T>, CancellationToken)Writes records as UTF-8 CSV data to a Stream asynchronously.
SerializeAsync<T>(TextWriter, IAsyncEnumerable<T>, CsvSerializerOptions?, CancellationToken)Writes records as CSV text to a TextWriter asynchronously, as they are produced.
SerializeAsync<T>(TextWriter, IAsyncEnumerable<T>, CsvTypeInfo<T>, CancellationToken)Writes records as CSV text to a TextWriter asynchronously, as they are produced.
SerializeAsync<T>(TextWriter, IEnumerable<T>, CsvSerializerOptions?, CancellationToken)Writes records as CSV text to a TextWriter asynchronously.
SerializeAsync<T>(TextWriter, IEnumerable<T>, CsvTypeInfo<T>, CancellationToken)Writes records as CSV text to a TextWriter asynchronously.
SerializeToUtf8Bytes<T>(IEnumerable<T>, CsvSerializerOptions?)Writes records as UTF-8 CSV data.
SerializeToUtf8Bytes<T>(IEnumerable<T>, CsvTypeInfo<T>)Writes records as UTF-8 CSV data.

Deserialize overloads

Deserialize<T>(Stream, CsvSerializerOptions?)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(Stream utf8Stream, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(Stream, CsvTypeInfo<T>)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream.

public static List<T> Deserialize<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(TextReader, CsvSerializerOptions?)

CsvSerializer.cs

Reads the records of CSV data from a TextReader.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(TextReader reader, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(TextReader, CsvTypeInfo<T>)

CsvSerializer.cs

Reads the records of CSV data from a TextReader.

public static List<T> Deserialize<T>(TextReader reader, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(ReadOnlyMemory<byte>, CsvSerializerOptions?)

CsvSerializer.cs

Reads the records of UTF-8 CSV data.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(ReadOnlyMemory<byte> utf8Csv, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
utf8Csv ReadOnlyMemory<byte>
The UTF-8 encoded CSV data.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(ReadOnlyMemory<byte>, CsvTypeInfo<T>)

CsvSerializer.cs

Reads the records of UTF-8 CSV data.

public static List<T> Deserialize<T>(ReadOnlyMemory<byte> utf8Csv, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
utf8Csv ReadOnlyMemory<byte>
The UTF-8 encoded CSV data.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(string, CsvSerializerOptions?)

CsvSerializer.cs

Reads the records of CSV data.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(string csv, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
csv string
The CSV data.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

Deserialize<T>(string, CsvTypeInfo<T>)

CsvSerializer.cs

Reads the records of CSV data.

public static List<T> Deserialize<T>(string csv, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
csv string
The CSV data.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

List<T> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

DeserializeAsync overloads

DeserializeAsync<T>(Stream, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream asynchronously.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task<List<T>> DeserializeAsync<T>(Stream utf8Stream, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task<List<T>> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

DeserializeAsync<T>(Stream, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream asynchronously.

public static Task<List<T>> DeserializeAsync<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task<List<T>> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

DeserializeAsync<T>(TextReader, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Reads the records of CSV data from a TextReader asynchronously.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task<List<T>> DeserializeAsync<T>(TextReader reader, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task<List<T>> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

DeserializeAsync<T>(TextReader, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Reads the records of CSV data from a TextReader asynchronously.

public static Task<List<T>> DeserializeAsync<T>(TextReader reader, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task<List<T>> The records.

Exceptions
CsvException
The data is invalid or a value cannot be converted.

DeserializeAsyncEnumerable overloads

DeserializeAsyncEnumerable<T>(Stream, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream one by one, asynchronously, as they are enumerated.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(Stream utf8Stream, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeAsyncEnumerable<T>(Stream, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream one by one, asynchronously, as they are enumerated.

public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeAsyncEnumerable<T>(TextReader, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Reads the records of CSV data from a TextReader one by one, asynchronously, as they are enumerated.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(TextReader reader, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeAsyncEnumerable<T>(TextReader, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Reads the records of CSV data from a TextReader one by one, asynchronously, as they are enumerated.

public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(TextReader reader, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeEnumerable overloads

DeserializeEnumerable<T>(Stream, CsvSerializerOptions?)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream one by one, as they are enumerated.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IEnumerable<T> DeserializeEnumerable<T>(Stream utf8Stream, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeEnumerable<T>(Stream, CsvTypeInfo<T>)

CsvSerializer.cs

Reads the records of UTF-8 CSV data from a Stream one by one, as they are enumerated.

public static IEnumerable<T> DeserializeEnumerable<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to read the UTF-8 data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeEnumerable<T>(TextReader, CsvSerializerOptions?)

CsvSerializer.cs

Reads the records of CSV data from a TextReader one by one, as they are enumerated.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IEnumerable<T> DeserializeEnumerable<T>(TextReader reader, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

DeserializeEnumerable<T>(TextReader, CsvTypeInfo<T>)

CsvSerializer.cs

Reads the records of CSV data from a TextReader one by one, as they are enumerated.

public static IEnumerable<T> DeserializeEnumerable<T>(TextReader reader, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
reader TextReader
The reader to read the data from. It is not disposed.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.

Serialize overloads

Serialize<T>(IBufferWriter<byte>, IEnumerable<T>, CsvSerializerOptions?)

CsvSerializer.cs

Writes records as UTF-8 CSV data to an IBufferWriter<T>.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static void Serialize<T>(IBufferWriter<byte> utf8BufferWriter, IEnumerable<T> values, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
utf8BufferWriter IBufferWriter<byte>
The buffer writer to write the UTF-8 data to.
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.

Serialize<T>(IBufferWriter<byte>, IEnumerable<T>, CsvTypeInfo<T>)

CsvSerializer.cs

Writes records as UTF-8 CSV data to an IBufferWriter<T>.

public static void Serialize<T>(IBufferWriter<byte> utf8BufferWriter, IEnumerable<T> values, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
utf8BufferWriter IBufferWriter<byte>
The buffer writer to write the UTF-8 data to.
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.

Serialize<T>(IEnumerable<T>, CsvSerializerOptions?)

CsvSerializer.cs

Writes records as CSV text.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static string Serialize<T>(IEnumerable<T> values, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

string The CSV text, with a header row when CsvSerializerOptions.HasHeaderRow is true.

Serialize<T>(IEnumerable<T>, CsvTypeInfo<T>)

CsvSerializer.cs

Writes records as CSV text.

public static string Serialize<T>(IEnumerable<T> values, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

string The CSV text, with a header row when CsvSerializerOptions.HasHeaderRow is true.

Serialize<T>(Stream, IEnumerable<T>, CsvSerializerOptions?)

CsvSerializer.cs

Writes records as UTF-8 CSV data to a Stream.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static void Serialize<T>(Stream utf8Stream, IEnumerable<T> values, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to write the UTF-8 data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.

Serialize<T>(Stream, IEnumerable<T>, CsvTypeInfo<T>)

CsvSerializer.cs

Writes records as UTF-8 CSV data to a Stream.

public static void Serialize<T>(Stream utf8Stream, IEnumerable<T> values, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to write the UTF-8 data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.

Serialize<T>(TextWriter, IEnumerable<T>, CsvSerializerOptions?)

CsvSerializer.cs

Writes records as CSV text to a TextWriter.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static void Serialize<T>(TextWriter writer, IEnumerable<T> values, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
writer TextWriter
The writer to write the data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.

Serialize<T>(TextWriter, IEnumerable<T>, CsvTypeInfo<T>)

CsvSerializer.cs

Writes records as CSV text to a TextWriter.

public static void Serialize<T>(TextWriter writer, IEnumerable<T> values, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
writer TextWriter
The writer to write the data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.

SerializeAsync overloads

SerializeAsync<T>(Stream, IAsyncEnumerable<T>, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Writes records as UTF-8 CSV data to a Stream asynchronously, as they are produced.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(Stream utf8Stream, IAsyncEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to write the UTF-8 data to. It is flushed, not disposed.
values IAsyncEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(Stream, IAsyncEnumerable<T>, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Writes records as UTF-8 CSV data to a Stream asynchronously, as they are produced.

public static Task SerializeAsync<T>(Stream utf8Stream, IAsyncEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to write the UTF-8 data to. It is flushed, not disposed.
values IAsyncEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(Stream, IEnumerable<T>, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Writes records as UTF-8 CSV data to a Stream asynchronously.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(Stream utf8Stream, IEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to write the UTF-8 data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(Stream, IEnumerable<T>, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Writes records as UTF-8 CSV data to a Stream asynchronously.

public static Task SerializeAsync<T>(Stream utf8Stream, IEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
utf8Stream Stream
The stream to write the UTF-8 data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(TextWriter, IAsyncEnumerable<T>, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Writes records as CSV text to a TextWriter asynchronously, as they are produced.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(TextWriter writer, IAsyncEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
writer TextWriter
The writer to write the data to. It is flushed, not disposed.
values IAsyncEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(TextWriter, IAsyncEnumerable<T>, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Writes records as CSV text to a TextWriter asynchronously, as they are produced.

public static Task SerializeAsync<T>(TextWriter writer, IAsyncEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
writer TextWriter
The writer to write the data to. It is flushed, not disposed.
values IAsyncEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(TextWriter, IEnumerable<T>, CsvSerializerOptions?, CancellationToken)

CsvSerializer.cs

Writes records as CSV text to a TextWriter asynchronously.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(TextWriter writer, IEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
writer TextWriter
The writer to write the data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeAsync<T>(TextWriter, IEnumerable<T>, CsvTypeInfo<T>, CancellationToken)

CsvSerializer.cs

Writes records as CSV text to a TextWriter asynchronously.

public static Task SerializeAsync<T>(TextWriter writer, IEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)
Type parameters
T
The type of the records.
Parameters
writer TextWriter
The writer to write the data to. It is flushed, not disposed.
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.
cancellationToken CancellationToken
The token to monitor for cancellation requests.
Returns

Task A task that represents the asynchronous operation.

SerializeToUtf8Bytes overloads

SerializeToUtf8Bytes<T>(IEnumerable<T>, CsvSerializerOptions?)

CsvSerializer.cs

Writes records as UTF-8 CSV data.

[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static byte[] SerializeToUtf8Bytes<T>(IEnumerable<T> values, CsvSerializerOptions? options = null)
Type parameters
T
The type of the records.
Parameters
values IEnumerable<T>
The records.
options CsvSerializerOptions?
The options, or null to use CsvSerializerOptions.Default.
Returns

byte[] The UTF-8 CSV data, without a byte order mark, with a header row when CsvSerializerOptions.HasHeaderRow is true.

Example
var people = new List<Person>
{
    new() { Name = "Alice", Age = 30 },
    new() { Name = "Bob", Age = 25 },
};

// The values are written as UTF-8 bytes, without creating a string first
byte[] utf8Csv = CsvSerializer.SerializeToUtf8Bytes(people);
Assert.Equal("Name,Age,BirthDate\r\nAlice,30,\r\nBob,25,\r\n"u8.ToArray(), utf8Csv);

var result = CsvSerializer.Deserialize<Person>(utf8Csv);
Assert.Equal("Bob", result[1].Name);

SerializeToUtf8Bytes<T>(IEnumerable<T>, CsvTypeInfo<T>)

CsvSerializer.cs

Writes records as UTF-8 CSV data.

public static byte[] SerializeToUtf8Bytes<T>(IEnumerable<T> values, CsvTypeInfo<T> typeInfo)
Type parameters
T
The type of the records.
Parameters
values IEnumerable<T>
The records.
typeInfo CsvTypeInfo<T>
The metadata of T.
Returns

byte[] The UTF-8 CSV data, without a byte order mark, with a header row when CsvSerializerOptions.HasHeaderRow is true.