Repository navigation
OnixLabs.Core.PreconditionInterpolatedStringHandler
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
|
[InterpolatedStringHandler]
[EditorBrowsable(EditorBrowsableState.Never)]
public ref struct PreconditionInterpolatedStringHandlerNote 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.
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.
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}.");
}| Constructor | Description |
|---|---|
PreconditionInterpolatedStringHandler(int, int, bool, out bool) |
Initializes a new handler for the specified precondition. Called by the compiler. |
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. |
| 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.
public void AppendLiteral(string value)Appends the specified literal text to the message.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The literal text to append. |
public void AppendFormatted<T>(T value)Appends the specified value to the message.
| Parameter | Type | Description |
|---|---|---|
value |
T |
The value to append. |
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. |
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. |
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. |
public void AppendFormatted(ReadOnlySpan<char> value)Appends the specified characters to the message.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The characters to append. |
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. |
public void AppendFormatted(string? value)Appends the specified string to the message.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The string to append. |
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. |
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. |
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions