TypeConverterTTryParse Method
Tries to parse a read-only span of characters as a T value.
Namespace: FolkerKinzel.CsvTools.Mappings.TypeConvertersAssembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
public abstract bool TryParse(
ReadOnlySpan<char> value,
out T? result
)
Public MustOverride Function TryParse (
value As ReadOnlySpan(Of Char),
<OutAttribute> ByRef result As T
) As Boolean
public:
virtual bool TryParse(
ReadOnlySpan<wchar_t> value,
[OutAttribute] T% result
) abstract
abstract TryParse :
value : ReadOnlySpan<char> *
result : 'T byref -> bool
- value ReadOnlySpanChar
- The read-only span of characters to parse.
- result T
-
After the method returns, contains the T value that is equivalent
to the parsed value, if the parsing succeeds, or the default value of
T if the parsing failed.
Booleantrue if the parsing was successfull, otherwise
false.
Implement this method in derived classes to determine the behavior of
Parse(ReadOnlySpanChar).
In any case the method MUST NOT throw an exception. Instead, it should return false
if parsing fails. In this case result is treated as undefined.
Writing an own implementation of TypeConverterT is easy:
using FolkerKinzel.CsvTools.Mappings.TypeConverters;
using System.Globalization;
namespace Examples;
/// <summary>
/// Example implementation of <see cref="TypeConverter{T}"/> for the
/// <see cref="Int128"/> struct.
/// </summary>
public sealed class Int128Converter() : TypeConverter<Int128>(true, default)
{
public override bool AcceptsNull => false;
public override string? ConvertToString(Int128 value)
=> value.ToString(null, CultureInfo.InvariantCulture);
public override bool TryParse(ReadOnlySpan<char> value, out Int128 result)
=> Int128.TryParse(value, out result);
}