TrueConditionInterpolatedStringHandler Struct

Provides an interpolated string handler that only performs formatting if a given condition is true.

This type is not available in the netstandard and .NET Framework builds of Louis, because their BCL has no StringBuilder.AppendInterpolatedStringHandler.

This type is only meant to be used by compiler-generated code. As such, it performs no parameter validation whatsoever.

public struct TrueConditionInterpolatedStringHandler

Example

The following code shows how this type can be used as an interpolated string handler:

using System;
using System.Runtime.CompilerServices;

public static class ConsoleUtility
{
    // Write a message to the console only if condition is true.
    // Usage example: ConsoleUtility.WriteLineIf(score > 100, $"Your score is {score}. That's awesome!");
    // String interpolation will only take place if necessary.
    public static void WriteLineIf(
        bool condition,
        [InterpolatedStringHandlerArgument(nameof(condition))] ref TrueConditionInterpolatedStringHandler message)
    {
        if (condition)
        {
            Console.WriteLine(message.ToStringAndClear());
        }
    }
}

Constructors

NameDescription
TrueConditionInterpolatedStringHandler(int, int, Param<bool>, bool)Initializes a new instance of the TrueConditionInterpolatedStringHandler struct, with the value of a Boolean parameter as the condition.
TrueConditionInterpolatedStringHandler(int, int, bool, bool)Initializes a new instance of the TrueConditionInterpolatedStringHandler struct.

TrueConditionInterpolatedStringHandler overloads

TrueConditionInterpolatedStringHandler(int, int, Param<bool>, bool)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Initializes a new instance of the TrueConditionInterpolatedStringHandler struct, with the value of a Boolean parameter as the condition.

public TrueConditionInterpolatedStringHandler(int literalLength, int formattedCount, Param<bool> param, out bool shouldAppend)
Parameters
literalLength int
The number of constant characters outside of interpolation expressions in the interpolated string.
formattedCount int
The number of interpolation expressions in the interpolated string.
param Param<bool>
The Param<T> whose value determines whether formatting will actually take place.
shouldAppend bool
A value indicating whether formatting should proceed.
Remarks

This constructor is only intended to be called by compiler-generated code. As such, it doesn't perform argument validation.

TrueConditionInterpolatedStringHandler(int, int, bool, bool)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Initializes a new instance of the TrueConditionInterpolatedStringHandler struct.

public TrueConditionInterpolatedStringHandler(int literalLength, int formattedCount, bool condition, out bool shouldAppend)
Parameters
literalLength int
The number of constant characters outside of interpolation expressions in the interpolated string.
formattedCount int
The number of interpolation expressions in the interpolated string.
condition bool
The condition that determines whether formatting will actually take place.
shouldAppend bool
A value indicating whether formatting should proceed.
Remarks

This constructor is only intended to be called by compiler-generated code. As such, it doesn't perform argument validation.

Methods

NameDescription
AppendFormatted(object?, int, string?)Writes the specified value to the handler.
AppendFormatted(ReadOnlySpan<char>)Writes the specified character span to the handler.
AppendFormatted(ReadOnlySpan<char>, int, string?)Writes the specified string of chars to the handler.
AppendFormatted(string?)Writes the specified value to the handler.
AppendFormatted(string?, int, string?)Writes the specified value to the handler.
AppendFormatted<T>(T)Writes the specified value to the handler.
AppendFormatted<T>(T, int)Writes the specified value to the handler.
AppendFormatted<T>(T, int, string?)Writes the specified value to the handler.
AppendFormatted<T>(T, string?)Writes the specified value to the handler.
AppendLiteral(string)Writes the specified string to the handler.
ToStringAndClear()Extracts the built string and frees any allocated memory.

AppendFormatted overloads

AppendFormatted(object?, int, string?)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted(object? value, int alignment = 0, string? format = null)
Parameters
value object?
The value to write.
alignment int
The minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.
format string?
The format string.

AppendFormatted(ReadOnlySpan<char>)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified character span to the handler.

public void AppendFormatted(ReadOnlySpan<char> value)
Parameters
value ReadOnlySpan<char>
The span to write.

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

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified string of chars to the handler.

public void AppendFormatted(ReadOnlySpan<char> value, int alignment = 0, string? format = null)
Parameters
value ReadOnlySpan<char>
The span to write.
alignment int
The minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.
format string?
The format string.

AppendFormatted(string?)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted(string? value)
Parameters
value string?
The value to write.

AppendFormatted(string?, int, string?)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted(string? value, int alignment = 0, string? format = null)
Parameters
value string?
The value to write.
alignment int
The minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.
format string?
The format string.

AppendFormatted<T>(T)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted<T>(T value)
Type parameters
T
The type of the value to write.
Parameters
value T
The value to write.

AppendFormatted<T>(T, int)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted<T>(T value, int alignment)
Type parameters
T
The type of the value to write.
Parameters
value T
The value to write.
alignment int
The minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.

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

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted<T>(T value, int alignment, string? format)
Type parameters
T
The type of the value to write.
Parameters
value T
The value to write.
alignment int
The minimum number of characters that should be written for this value. If the value is negative, it indicates left-aligned and the required minimum is the absolute value.
format string?
The format string.

AppendFormatted<T>(T, string?)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified value to the handler.

public void AppendFormatted<T>(T value, string? format)
Type parameters
T
The type of the value to write.
Parameters
value T
The value to write.
format string?
The format string.

AppendLiteral(string)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Writes the specified string to the handler.

public void AppendLiteral(string value)

Parameters

value string
The string to write.

ToStringAndClear()

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Extracts the built string and frees any allocated memory.

public string ToStringAndClear()

Returns

string The built string.