TypeConverterTTryParse Method

Tries to parse a read-only span of characters as a T value.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings.TypeConverters
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public abstract bool TryParse(
	ReadOnlySpan<char> value,
	out T? result
)

Parameters

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.

Return Value

Boolean
true if the parsing was successfull, otherwise false.

Remarks

  Notes to Implementers

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.

Example

Writing an own implementation of TypeConverterT is easy:

C#
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);
}

See Also