Skip to content

OnixLabs.Core.PreconditionInterpolatedStringHandler

Matthew Layton edited this page Nov 15, 2026 · 1 revision

PreconditionInterpolatedStringHandler Struct

Represents an interpolated string handler for precondition messages, which formats the message only when the precondition fails.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ValueType → PreconditionInterpolatedStringHandler

Definition

[InterpolatedStringHandler]
[EditorBrowsable(EditorBrowsableState.Never)]
public ref struct PreconditionInterpolatedStringHandler

Note This struct is decorated with [EditorBrowsable(EditorBrowsableState.Never)], which hides it from IntelliSense completion lists in some IDE configurations. It is used automatically by the compiler for every interpolated string passed as a precondition message; callers never construct it themselves.

Remarks

A precondition such as Require(found, $"Entry '{name}' not found in {typeof(T).Name}.") would otherwise format its message on every call, including the vast majority that succeed. Preconditions.Check(bool, ref PreconditionInterpolatedStringHandler) and Preconditions.Require(bool, ref PreconditionInterpolatedStringHandler, string?) take their message as this handler, with [InterpolatedStringHandlerArgument(nameof(condition))], so the compiler passes the condition to the handler's constructor.

When the condition is true, the constructor reports that nothing should be appended, so no formatting happens at all and the expressions inside the {…} holes are not evaluated, in the same way as System.Diagnostics.Debug.Assert(bool). When the condition is false, the message is built with a DefaultInterpolatedStringHandler and used as the exception message.

Because PreconditionInterpolatedStringHandler is a ref struct, it cannot be boxed, stored in a field of a class, or captured by a lambda.

Examples

Lazy precondition messages

using static OnixLabs.Core.Preconditions;

public void Withdraw(Account account, decimal amount)
{
    // When the balance is sufficient, the message is not formatted and account.Describe() is not called.
    Check(account.Balance >= amount, $"Insufficient funds in {account.Describe()} to withdraw {amount:C}.");
}

Constructors

Constructor Description
PreconditionInterpolatedStringHandler(int, int, bool, out bool) Initializes a new handler for the specified precondition. Called by the compiler.

PreconditionInterpolatedStringHandler(int, int, bool, out bool)

public PreconditionInterpolatedStringHandler(int literalLength, int formattedCount, bool condition, out bool shouldAppend)

Initializes a new instance of the PreconditionInterpolatedStringHandler struct.

Parameter Type Description
literalLength int The number of constant characters outside the interpolation expressions.
formattedCount int The number of interpolation expressions.
condition bool The precondition being checked; the message is only formatted when it is false.
shouldAppend out bool When this constructor returns, contains a value indicating whether the message should be formatted; true only when condition is false.

Methods

Method Returns Description
AppendLiteral(string) void Appends the specified literal text to the message.
AppendFormatted<T>(T) void Appends the specified value to the message.
AppendFormatted<T>(T, string?) void Appends the specified value to the message, using the specified format.
AppendFormatted<T>(T, int) void Appends the specified value to the message, using the specified alignment.
AppendFormatted<T>(T, int, string?) void Appends the specified value to the message, using the specified alignment and format.
AppendFormatted(ReadOnlySpan<char>) void Appends the specified characters to the message.
AppendFormatted(ReadOnlySpan<char>, int, string?) void Appends the specified characters to the message, using the specified alignment and format.
AppendFormatted(string?) void Appends the specified string to the message.
AppendFormatted(string?, int, string?) void Appends the specified string to the message, using the specified alignment and format.
AppendFormatted(object?, int, string?) void Appends the specified object to the message, using the specified alignment and format.

The compiler calls these methods only when the constructor sets shouldAppend to true. They forward to the corresponding DefaultInterpolatedStringHandler methods.


AppendLiteral(string)

public void AppendLiteral(string value)

Appends the specified literal text to the message.

Parameter Type Description
value string The literal text to append.

AppendFormatted<T>(T)

public void AppendFormatted<T>(T value)

Appends the specified value to the message.

Parameter Type Description
value T The value to append.

AppendFormatted<T>(T, string?)

public void AppendFormatted<T>(T value, string? format)

Appends the specified value to the message, using the specified format.

Parameter Type Description
value T The value to append.
format string? The format string.

AppendFormatted<T>(T, int)

public void AppendFormatted<T>(T value, int alignment)

Appends the specified value to the message, using the specified alignment.

Parameter Type Description
value T The value to append.
alignment int The minimum width; a negative value left-aligns the value.

AppendFormatted<T>(T, int, string?)

public void AppendFormatted<T>(T value, int alignment, string? format)

Appends the specified value to the message, using the specified alignment and format.

Parameter Type Description
value T The value to append.
alignment int The minimum width; a negative value left-aligns the value.
format string? The format string.

AppendFormatted(ReadOnlySpan<char>)

public void AppendFormatted(ReadOnlySpan<char> value)

Appends the specified characters to the message.

Parameter Type Description
value ReadOnlySpan<char> The characters to append.

AppendFormatted(ReadOnlySpan<char>, int, string?)

public void AppendFormatted(ReadOnlySpan<char> value, int alignment = 0, string? format = null)

Appends the specified characters to the message, using the specified alignment and format.

Parameter Type Description
value ReadOnlySpan<char> The characters to append.
alignment int The minimum width; a negative value left-aligns the value.
format string? The format string, which is ignored for characters.

AppendFormatted(string?)

public void AppendFormatted(string? value)

Appends the specified string to the message.

Parameter Type Description
value string? The string to append.

AppendFormatted(string?, int, string?)

public void AppendFormatted(string? value, int alignment = 0, string? format = null)

Appends the specified string to the message, using the specified alignment and format.

Parameter Type Description
value string? The string to append.
alignment int The minimum width; a negative value left-aligns the value.
format string? The format string, which is ignored for strings.

AppendFormatted(object?, int, string?)

public void AppendFormatted(object? value, int alignment = 0, string? format = null)

Appends the specified object to the message, using the specified alignment and format.

Parameter Type Description
value object? The object to append.
alignment int The minimum width; a negative value left-aligns the value.
format string? The format string.

See Also

ONIXLabs .NET Library


Clone this wiki locally