Skip to main content
Version: 0.3.x (0.3.0)

Class UnderlyingValue

Namespace: AdCodicem.ValueObjects
Assembly: AdCodicem.ValueObjects.Abstractions.dll

Thin generic bridges to the underlying type's own parsing and formatting, used by generated code.

public static class UnderlyingValue

Inheritance​

object ← UnderlyingValue

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

Going through a constrained generic call rather than naming Guid.TryParse, Decimal.TryParse and the rest one by one keeps the generator honest: any type reachable here is guaranteed by the compiler to expose the API, and the JIT devirtualizes the call for the concrete type argument, so nothing is paid at run time for the indirection.

Methods​

Abs<TValue>(TValue)​

Returns the absolute value of a numeric underlying value.

public static TValue Abs<TValue>(TValue value) where TValue : INumberBase<TValue>

Parameters​

value TValue

Value to take the absolute value of.

Returns​

TValue

The absolute value.

Type Parameters​

TValue

Underlying value type.

Remarks​

The numeric constants of the BCL primitives are static abstract members of INumberBase, reachable only through a type parameter. These bridges are how generated code names them.

IsZero<TValue>(TValue)​

Determines whether a numeric underlying value is zero.

public static bool IsZero<TValue>(TValue value) where TValue : INumberBase<TValue>

Parameters​

value TValue

Value to test.

Returns​

bool

true when the value is zero.

Type Parameters​

TValue

Underlying value type.

Remarks​

The numeric constants of the BCL primitives are static abstract members of INumberBase, reachable only through a type parameter. These bridges are how generated code names them.

One<TValue>()​

Gets the multiplicative identity of a numeric underlying type.

public static TValue One<TValue>() where TValue : INumberBase<TValue>

Returns​

TValue

One.

Type Parameters​

TValue

Underlying value type.

Remarks​

The numeric constants of the BCL primitives are static abstract members of INumberBase, reachable only through a type parameter. These bridges are how generated code names them.

TryConvertToInteger<TNumber, TValue>(TNumber, out TValue)​

Converts a number to an integer type with a checked conversion that never truncates.

public static bool TryConvertToInteger<TNumber, TValue>(TNumber number, out TValue value) where TNumber : INumberBase<TNumber> where TValue : IBinaryInteger<TValue>

Parameters​

number TNumber

Number to convert.

value TValue

The same number, as a TValue.

Returns​

bool

true when TValue holds the number whole; false when the number is out of its range, has a fraction, or is not a number at all.

Type Parameters​

TNumber

Type of the number to convert.

TValue

Integer type to convert it to.

Remarks​

The type converter of a numeric value object goes through it for a number of another numeric type than its own, such as the Newtonsoft.Json reads every JSON integer as, or the a numeric control holds, and to hand its value to such a type. A checked cast alone would drop a fraction, 7.5 becoming 7, and that is refused too.

TryConvertToReal<TNumber, TValue>(TNumber, out TValue)​

Converts a number to a real type with a checked conversion.

public static bool TryConvertToReal<TNumber, TValue>(TNumber number, out TValue value) where TNumber : INumberBase<TNumber> where TValue : IFloatingPoint<TValue>

Parameters​

number TNumber

Number to convert.

value TValue

The nearest TValue to the number.

Returns​

bool

true when the number is within the range of TValue; false when it is beyond it, or is NaN or an infinity, which a does not hold.

Type Parameters​

TNumber

Type of the number to convert.

TValue

Real type to convert it to: , or .

Remarks​

The type converter of a numeric value object goes through it as it goes through . A real holds the number to its own precision, as a cast does: 0.1 converted to a is the nearest . A or a converted to a is the one exception: a cast keeps fifteen significant digits of a and seven of a , fewer than either carries, so that 16777216f would become 16777220. It is read from its shortest round-trip text instead, which keeps every digit it holds, as the Newtonsoft.Json converter reads a JSON number Newtonsoft.Json turned into a . What the bridge refuses is a number beyond the range of the real: a has no infinity to hold one, and a beyond the range of a would otherwise become an infinity, which is not the number either. NaN and an infinity carry over to a or a as they are, and a refuses them.

TryFormat<TValue>(in TValue, Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)​

Formats a value into a destination span using the underlying type's own formatter.

public static bool TryFormat<TValue>(in TValue value, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) where TValue : ISpanFormattable

Parameters​

value TValue

Value to format.

destination Span<char>

Destination buffer.

charsWritten int

Number of characters written.

format ReadOnlySpan<char>

Format specifier.

provider IFormatProvider?

Format provider.

Returns​

bool

true when the value fitted in destination.

Type Parameters​

TValue

Underlying value type.

Remarks​

Several BCL types, among them, implement the four-argument explicitly and only expose a shorter public overload. Routing through a constrained type parameter reaches the interface method uniformly, and the call is devirtualized for the concrete type argument.

TryParse<TValue>(ReadOnlySpan<char>, IFormatProvider?, out TValue)​

Parses a span using the underlying type's own parser.

public static bool TryParse<TValue>(ReadOnlySpan<char> text, IFormatProvider? provider, out TValue value) where TValue : ISpanParsable<TValue>

Parameters​

text ReadOnlySpan<char>

Text to parse.

provider IFormatProvider?

Format provider.

value TValue

The parsed value.

Returns​

bool

true when the text was parsed.

Type Parameters​

TValue

Underlying value type.

TryParse<TValue>(string?, IFormatProvider?, out TValue)​

Parses a string using the underlying type's own parser.

public static bool TryParse<TValue>(string? text, IFormatProvider? provider, out TValue value) where TValue : IParsable<TValue>

Parameters​

text string?

Text to parse.

provider IFormatProvider?

Format provider.

value TValue

The parsed value.

Returns​

bool

true when the text was parsed.

Type Parameters​

TValue

Underlying value type.

TryParse<TValue>(ReadOnlySpan<char>, NumberStyles, IFormatProvider?, out TValue)​

Parses a span using the underlying number type's own parser, with the given styles in the invariant culture.

public static bool TryParse<TValue>(ReadOnlySpan<char> text, NumberStyles invariantStyle, IFormatProvider? provider, out TValue value) where TValue : INumberBase<TValue>

Parameters​

text ReadOnlySpan<char>

Text to parse.

invariantStyle NumberStyles

The styles the text is read with when provider is null or the invariant culture.

provider IFormatProvider?

Format provider. null stands for ; any culture other than the invariant one reads the text with the type's own styles.

value TValue

The parsed value.

Returns​

bool

true when the text was parsed.

Type Parameters​

TValue

Underlying value type.

Remarks​

The own styles of , and accept the group separator, a comma in the invariant culture, so text written with a decimal comma, 12,5, would read as 125. Generated code hands those styles over without . A culture that is not the invariant one says what a comma is, and keeps the type's own styles: fr-FR reads 12,5 as 12.5. The invariant culture is recognized by equality, so a created for the empty name is read as is, and so is its number format, , which callers of decimal.Parse pass as often.

Zero<TValue>()​

Gets the additive identity of a numeric underlying type.

public static TValue Zero<TValue>() where TValue : INumberBase<TValue>

Returns​

TValue

Zero.

Type Parameters​

TValue

Underlying value type.

Remarks​

The numeric constants of the BCL primitives are static abstract members of INumberBase, reachable only through a type parameter. These bridges are how generated code names them.