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 TrueConditionInterpolatedStringHandlerExample
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
| Name | Description |
|---|---|
| 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
literalLengthint- The number of constant characters outside of interpolation expressions in the interpolated string.
formattedCountint- The number of interpolation expressions in the interpolated string.
paramParam<bool>- The
Param<T>whose value determines whether formatting will actually take place. shouldAppendbool- 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
literalLengthint- The number of constant characters outside of interpolation expressions in the interpolated string.
formattedCountint- The number of interpolation expressions in the interpolated string.
conditionbool- The condition that determines whether formatting will actually take place.
shouldAppendbool- 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
| Name | Description |
|---|---|
| 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.
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
valueReadOnlySpan<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
valueReadOnlySpan<char>- The span to write.
alignmentint- 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.
formatstring?- The format string.
AppendFormatted(string?)
Available in: net10.0net11.0(not available in the other target frameworks of the package)
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.
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
valueT- 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.
Type parameters
T- The type of the value to write.
Parameters
valueT- The value to write.
alignmentint- 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.
Type parameters
T- The type of the value to write.
Parameters
AppendFormatted<T>(T, string?)
Available in: net10.0net11.0(not available in the other target frameworks of the package)
AppendLiteral(string)
Available in: net10.0net11.0(not available in the other target frameworks of the package)
ToStringAndClear()
Available in: net10.0net11.0(not available in the other target frameworks of the package)