public static class CsvEnumExtensions
Namespace
CsvHelper
Assembly
CsvHelper.dll
Builds
net10.0 netstandard2.1 netstandard2.0 net47

Remarks

On .NET Framework and .NET Standard, where Parse<T> has no constraint, the methods of the reader, its rows and fields, and dynamic records parse enums themselves, the same way, and this class does not exist.

Methods

Parse<TEnum>(CsvField)

net10.0 onlyAdded in v34.0.0static
public static TEnum Parse<TEnum>(this CsvField field) where TEnum : struct, Enum

Parses the value of the field as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

fieldCsvField
The field.

Returns

TEnum
The member of TEnum.

Exceptions

FormatException
The value is not a value of TEnum.

View source

Parse<TEnum>(CsvDynamicRecord, string)

net10.0 onlyAdded in v34.0.0static
public static TEnum Parse<TEnum>(this CsvDynamicRecord record, string name) where TEnum : struct, Enum

Parses the field of the column called name as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

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

Returns

TEnum
The member of TEnum.

Exceptions

ArgumentNullException
record or name is null.
KeyNotFoundException
No column has that name.
FormatException
The field is not a value of TEnum.
IndexOutOfRangeException
The record has no field in that column and missing fields throw.

View source

Parse<TEnum>(CsvReader, int)

net10.0 onlyAdded in v34.0.0static
public static TEnum Parse<TEnum>(this CsvReader reader, int index) where TEnum : struct, Enum

Parses field index as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

readerCsvReader
The reader.
indexint
The 0-based index of the field.

Returns

TEnum
The member of TEnum.

Exceptions

ArgumentNullException
reader is null.
FormatException
The field is not a value of TEnum.
ArgumentOutOfRangeException
index is negative.

View source

Parse<TEnum>(CsvReader, string)

net10.0 onlyAdded in v34.0.0static
public static TEnum Parse<TEnum>(this CsvReader reader, string name) where TEnum : struct, Enum

Parses the field with the given header name as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

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

Returns

TEnum
The member of TEnum.

Exceptions

ArgumentNullException
reader or name is null.
KeyNotFoundException
No column has that name.
FormatException
The field is not a value of TEnum.

View source

Parse<TEnum>(CsvRow, int)

net10.0 onlyAdded in v34.0.0static
public static TEnum Parse<TEnum>(this CsvRow row, int index) where TEnum : struct, Enum

Parses field index of the row as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

rowCsvRow
The row.
indexint
The 0-based index of the field.

Returns

TEnum
The member of TEnum.

Exceptions

FormatException
The field is not a value of TEnum.
ArgumentOutOfRangeException
index is negative.

View source

ParseOrNull<TEnum>(CsvField)

net10.0 onlyAdded in v34.0.0static
public static TEnum? ParseOrNull<TEnum>(this CsvField field) where TEnum : struct, Enum

Parses the value of the field as the enum TEnum, or returns null if it is empty, one of CsvReaderOptions.NullValues or not a value of TEnum.

Type parameters

TEnum
The enum type.

Parameters

fieldCsvField
The field.

Returns

TEnum?
The member, or null when the value is null or not a value of TEnum.

View source

TryParse<TEnum>(CsvField, out TEnum)

net10.0 onlyAdded in v34.0.0static
public static bool TryParse<TEnum>(this CsvField field, out TEnum value) where TEnum : struct, Enum

Tries to parse the value of the field as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

fieldCsvField
The field.
valueTEnum
The member, or the default of TEnum when the value is not a value of it.

Returns

bool
Whether the value could be parsed.

View source

TryParse<TEnum>(CsvReader, int, out TEnum)

net10.0 onlyAdded in v34.0.0static
public static bool TryParse<TEnum>(this CsvReader reader, int index, out TEnum value) where TEnum : struct, Enum

Tries to parse field index as the enum TEnum.

Type parameters

TEnum
The enum type.

Parameters

readerCsvReader
The reader.
indexint
The 0-based index of the field.
valueTEnum
The member, or the default of TEnum when the field is not a value of it.

Returns

bool
Whether the field could be parsed.

Exceptions

ArgumentNullException
reader is null.
ArgumentOutOfRangeException
index is negative.

View source