ISpanFormattable.TryFormat Method

Definition

Tries to format the value of the current instance into the provided span of characters.

C#
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
Int32

When 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
IFormatProvider

An optional object that supplies culture-specific formatting information for destination.

Returns

true if the formatting was successful; otherwise, false.

Remarks

  • An implementation of this interface should produce the same string of characters as an implementation of ToString(String, IFormatProvider) on the same type.
  • TryFormat should return false only if there is not enough space in the destination buffer. Any other failures should throw an exception.

Applies to

Product Versions
.NET 6, 7, 8, 9