Method TryFormat
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Tries to format the value of the current instance into the provided span of characters.
[Pure]
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
destination
Span<char>The span in which to write this instance's value formatted as a span of characters.
charsWritten
intWhen this method returns, contains the number of characters that were written in
destination
.format
ReadOnlySpan<char>A span containing the characters that represent a standard or custom format string that defines the acceptable format for
destination
.provider
IFormatProviderAn optional object that supplies culture-specific formatting information for
destination
.
Returns
Remarks
This method should be used only when absolutely necessary, because it always has more overhead than
ToString(string?, IFormatProvider?), as this method results in an intermediate allocation
of one or more instances of string and a copy of that string to
destination
if formatting was successful.
When possible, use
ToString(string?, IFormatProvider?), which attempts to avoid intermediate allocations.
This method only returns true and with its output written to destination
if the formatted string, in its entirety, will fit in destination
. If the resulting
formatted string is too large to fit in destination
, the result will be false and
destination
will be unaltered.
The resulting formatted string may be shorter than destination
. When this method
returns true, use charsWritten
when handling the value of
destination
.
TryFormat(Span<byte>, out int, ReadOnlySpan<char>, IFormatProvider?)
Tries to format the value of the current instance as UTF-8 into the provided span of bytes.
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
utf8Destination
Span<byte>The span in which to write this instance's value formatted as a span of bytes.
bytesWritten
intWhen this method returns, contains the number of bytes that were written in
utf8Destination
.format
ReadOnlySpan<char>A span containing the characters that represent a standard or custom format string that defines the acceptable format for
utf8Destination
.provider
IFormatProviderAn optional object that supplies culture-specific formatting information for
utf8Destination
.
Returns
Remarks
Use of this method involves a stack allocation of utf8Destination
.Length * 2 bytes. Use of
the overload taking a char span is recommended.