StringBuilder.AppendFormat Method

Definition

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding object argument.

Overloads

AppendFormat(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

AppendFormat(String, Object, Object, Object)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of three arguments.

AppendFormat(IFormatProvider, String, Object, Object)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of two arguments using a specified format provider.

AppendFormat(String, Object, Object)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of two arguments.

AppendFormat(IFormatProvider, String, Object, Object, Object)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of three arguments using a specified format provider.

AppendFormat(IFormatProvider, CompositeFormat, Object[])

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

AppendFormat(String, ReadOnlySpan<Object>)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter span.

AppendFormat(IFormatProvider, String, Object[])

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array using a specified format provider.

AppendFormat(IFormatProvider, String, Object)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a single argument using a specified format provider.

AppendFormat(String, Object[])

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array.

AppendFormat(String, Object)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a single argument.

AppendFormat(IFormatProvider, String, ReadOnlySpan<Object>)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter span using a specified format provider.

AppendFormat<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

AppendFormat<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

AppendFormat<TArg0>(IFormatProvider, CompositeFormat, TArg0)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

AppendFormat(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, System.Text.CompositeFormat format, ReadOnlySpan<object?> args);
C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, System.Text.CompositeFormat format, scoped ReadOnlySpan<object?> args);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

args
ReadOnlySpan<Object>

A span of objects to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format is null.

The index of a format item is greater than or equal to the number of supplied arguments.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

AppendFormat(String, Object, Object, Object)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of three arguments.

C#
public System.Text.StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2);
C#
public System.Text.StringBuilder AppendFormat(string format, object? arg0, object? arg1, object? arg2);

Parameters

format
String

A composite format string.

arg0
Object

The first object to format.

arg1
Object

The second object to format.

arg2
Object

The third object to format.

Returns

A reference to this instance with format appended. Each format item in format is replaced by the string representation of the corresponding object argument.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to 3.

The length of the expanded string would exceed MaxCapacity.

Examples

The following example demonstrates the AppendFormat method.

C#
using System;
using System.Text;
using System.Globalization;

class Sample
{
    static StringBuilder sb = new StringBuilder();

    public static void Main()
    {
    int    var1   = 111;
    float  var2   = 2.22F;
    string var3   = "abcd";
    object[] var4 = {3, 4.4, 'X'};

    Console.WriteLine();
    Console.WriteLine("StringBuilder.AppendFormat method:");
    sb.AppendFormat("1) {0}", var1);
    Show(sb);
    sb.AppendFormat("2) {0}, {1}", var1, var2);
    Show(sb);
    sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
    Show(sb);
    sb.AppendFormat("4) {0}, {1}, {2}", var4);
    Show(sb);
    CultureInfo ci = new CultureInfo("es-ES", true);
    sb.AppendFormat(ci, "5) {0}", var2);
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    Console.WriteLine(sbs.ToString());
    sb.Length = 0;
    }
}
/*
This example produces the following results:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to arg0 through arg2, the objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

arg0, arg1, and arg2 represent the objects to be formatted. Each format item in format is replaced with the string representation of either arg0, arg1, or arg2, depending on the index of the format item. If the format item includes formatString and the corresponding object in args implements the IFormattable interface, then argx.ToString(formatString, null) defines the formatting, where x is the index of the argument. Otherwise, argx.ToString() defines the formatting.

If the string assigned to format is "Thank you for your donation of {0:####} cans of food to our charitable organization." and arg0 is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(IFormatProvider, String, Object, Object)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of two arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0, object arg1);
C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, string format, object? arg0, object? arg1);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

format
String

A composite format string.

arg0
Object

The first object to format.

arg1
Object

The second object to format.

Returns

A reference to this instance after the append operation has completed. After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format where any format specification is replaced by the string representation of the corresponding object argument.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to 2 (two).

The length of the expanded string would exceed MaxCapacity.

Examples

The following example uses the AppendFormat(IFormatProvider, String, Object, Object) method to display time and temperature data stored in a generic Dictionary<TKey,TValue> object. Note that the format string has three format items, although there are only to objects to format. This is because the first object in the list (a date and time value) is used by two format items: The first format item displays the time, and the second displays the date.

C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = new CultureInfo("en-US");
      StringBuilder sb = new StringBuilder();
      Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>(); 
      temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46);
      temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);

      sb.AppendLine("Temperature Information:\n");
      foreach (var item in temperatureInfo)
      {
         sb.AppendFormat(culture,
                         "Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F\n",
                         item.Key, item.Value);
      }
      Console.WriteLine(sb.ToString());
   }
}
// The example displays the following output:
//       Temperature Information:
//       
//       Temperature at  2:00 PM on  6/1/2010:  87.5°F
//       Temperature at 10:00 AM on 12/1/2010:  36.8°F

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

The provider parameter specifies an IFormatProvider implementation that can provide formatting information for arg0 and arg1. provider can be any of the following:

  • A CultureInfo object that provides culture-specific formatting information.

  • A NumberFormatInfo object that provides culture-specific formatting information for arg0 or arg1 if they are numeric values.

  • A DateTimeFormatInfo object that provides culture-specific formatting information for arg0 or arg1 if they are date and time values.

  • A custom IFormatProvider implementation that provides formatting information for arg0 and arg1. Typically, such an implementation also implements the ICustomFormatter interface.

If the provider parameter is null, format provider information is obtained from the current culture.

arg0 and arg1 represent the objects to be formatted. Each format item in format is replaced with the string representation of the object that has the corresponding index. If the format item includes formatString and the corresponding argument implements the IFormattable interface, then the argument's ToString(formatString, provider) method defines the formatting. Otherwise, the argument's ToString() method defines the formatting.

Notes to Callers

IIn .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(String, Object, Object)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of two arguments.

C#
public System.Text.StringBuilder AppendFormat(string format, object arg0, object arg1);
C#
public System.Text.StringBuilder AppendFormat(string format, object? arg0, object? arg1);

Parameters

format
String

A composite format string.

arg0
Object

The first object to format.

arg1
Object

The second object to format.

Returns

A reference to this instance with format appended. Each format item in format is replaced by the string representation of the corresponding object argument.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to 2.

The length of the expanded string would exceed MaxCapacity.

Examples

The following example demonstrates the AppendFormat method.

C#
using System;
using System.Text;
using System.Globalization;

class Sample
{
    static StringBuilder sb = new StringBuilder();

    public static void Main()
    {
    int    var1   = 111;
    float  var2   = 2.22F;
    string var3   = "abcd";
    object[] var4 = {3, 4.4, 'X'};

    Console.WriteLine();
    Console.WriteLine("StringBuilder.AppendFormat method:");
    sb.AppendFormat("1) {0}", var1);
    Show(sb);
    sb.AppendFormat("2) {0}, {1}", var1, var2);
    Show(sb);
    sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
    Show(sb);
    sb.AppendFormat("4) {0}, {1}, {2}", var4);
    Show(sb);
    CultureInfo ci = new CultureInfo("es-ES", true);
    sb.AppendFormat(ci, "5) {0}", var2);
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    Console.WriteLine(sbs.ToString());
    sb.Length = 0;
    }
}
/*
This example produces the following results:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to arg0 and arg1, the two objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

arg0 and arg1 represent the objects to be formatted. Each format item in format is replaced with the string representation of either arg0 or arg1. If the format item includes formatString and the corresponding object implements the IFormattable interface, then argx.ToString(formatString, provider) defines the formatting, where x is the index of the argument. Otherwise, argx.ToString() defines the formatting.

If the string assigned to format is "Thank you for your donation of {0:####} cans of food to our charitable organization." and arg0 is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(IFormatProvider, String, Object, Object, Object)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of either of three arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0, object arg1, object arg2);
C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

format
String

A composite format string.

arg0
Object

The first object to format.

arg1
Object

The second object to format.

arg2
Object

The third object to format.

Returns

A reference to this instance after the append operation has completed. After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format where any format specification is replaced by the string representation of the corresponding object argument.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to 3 (three).

The length of the expanded string would exceed MaxCapacity.

Examples

The following example uses the AppendFormat(IFormatProvider, String, Object, Object, Object) method to illustrate the result of a Boolean And operation with integer values. Note that the format string includes six format items, but the method has only three items in its argument list, because each item is formatted in two different ways.

C#
using System;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
      StringBuilder sb = new StringBuilder();
      string formatString = "    {0,12:N0} ({0,8:X8})\n" +
                            "And {1,12:N0} ({1,8:X8})\n" +
                            "  = {2,12:N0} ({2,8:X8})\n";
      for (int ctr = 0; ctr <= 2; ctr++) {
         int value1 = rnd.Next();
         int value2 = rnd.Next();
         sb.AppendFormat(culture, formatString,
                         value1, value2, value1 & value2).
                         AppendLine();
      }
      Console.WriteLine(sb.ToString());
   }
}
// The example displays output like the following:
//           1 984 112 195 (76432643)
//       And 1 179 778 511 (4651FDCF)
//         = 1 178 674 243 (46412443)
//
//           2 034 813 710 (7948CB0E)
//       And  569 333 976 (21EF58D8)
//         =  558 385 160 (21484808)
//
//            126 717 735 (078D8F27)
//       And 1 830 715 973 (6D1E8245)
//         =   84 705 797 (050C8205)

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

The provider parameter specifies an IFormatProvider implementation that can provide formatting information for arg0 and arg1. provider can be any of the following:

  • A CultureInfo object that provides culture-specific formatting information.

  • A NumberFormatInfo object that provides culture-specific formatting information for arg0 or arg1 if they are numeric values.

  • A DateTimeFormatInfo object that provides culture-specific formatting information for arg0, arg1, or arg2 if they are date and time values.

  • A custom IFormatProvider implementation that provides formatting information for arg0, arg1, and arg2. Typically, such an implementation also implements the ICustomFormatter interface.

If the provider parameter is null, format provider information is obtained from the current culture.

arg0, arg1, and arg2 represent the objects to be formatted. Each format item in format is replaced with the string representation of the object that has the corresponding index. If the format item includes formatString and the corresponding argument implements the IFormattable interface, then the argument's ToString(formatString, provider) method defines the formatting. Otherwise, the argument's ToString() method defines the formatting.

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(IFormatProvider, CompositeFormat, Object[])

Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, System.Text.CompositeFormat format, params object?[] args);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

args
Object[]

An array of objects to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format or args is null.

The index of a format item is greater than or equal to the number of supplied arguments.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

AppendFormat(String, ReadOnlySpan<Object>)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter span.

C#
public System.Text.StringBuilder AppendFormat(string format, scoped ReadOnlySpan<object?> args);

Parameters

format
String

A composite format string.

args
ReadOnlySpan<Object>

A span of objects to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format is null.

The length of the expanded string would exceed MaxCapacity.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the length of the args span.

Applies to

.NET 9
Product Versions
.NET 9

AppendFormat(IFormatProvider, String, Object[])

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args);
C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, string format, params object?[] args);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

format
String

A composite format string.

args
Object[]

An array of objects to format.

Returns

A reference to this instance after the append operation has completed. After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format where any format specification is replaced by the string representation of the corresponding object argument.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the length of the args array.

The length of the expanded string would exceed MaxCapacity.

Examples

The following example demonstrates the AppendFormat method.

C#
using System;
using System.Text;
using System.Globalization;

class Sample
{
    static StringBuilder sb = new StringBuilder();

    public static void Main()
    {
    int    var1   = 111;
    float  var2   = 2.22F;
    string var3   = "abcd";
    object[] var4 = {3, 4.4, 'X'};

    Console.WriteLine();
    Console.WriteLine("StringBuilder.AppendFormat method:");
    sb.AppendFormat("1) {0}", var1);
    Show(sb);
    sb.AppendFormat("2) {0}, {1}", var1, var2);
    Show(sb);
    sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
    Show(sb);
    sb.AppendFormat("4) {0}, {1}, {2}", var4);
    Show(sb);
    CultureInfo ci = new CultureInfo("es-ES", true);
    sb.AppendFormat(ci, "5) {0}", var2);
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    Console.WriteLine(sbs.ToString());
    sb.Length = 0;
    }
}
/*
This example produces the following results:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/

The following example defines a custom IFormatProvider implementation named CustomerFormatter that formats a 10-digit customer number with hyphens after the fourth and seventh digits. It is passed to the StringBuilder.AppendFormat(IFormatProvider, String, Object[]) method to create a string that includes the formatted customer number and customer name.

C#
using System;
using System.Text;

public class Customer
{
   private string custName;
   private int custNumber;
   
   public Customer(string name, int number)
   {
      this.custName = name;
      this.custNumber = number;
   }
   
   public string Name
   {
      get { return this.custName; }
   }
   
   public int CustomerNumber
   {
      get { return this.custNumber; }
   }
}

public class CustomerNumberFormatter : IFormatProvider, ICustomFormatter
{   
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(ICustomFormatter))
         return this;
      return null;
   }
   
   public string Format(string format, object arg, IFormatProvider provider)
   {
      if (arg is Int32)
      {
         string custNumber = ((int) arg).ToString("D10");
         return custNumber.Substring(0, 4) + "-" + custNumber.Substring(4, 3) + 
                "-" + custNumber.Substring(7, 3);
      }
      else
      {
         return null;
      }
   }                   
}

public class Example
{
   public static void Main()
   {
      Customer customer = new Customer("A Plus Software", 903654);
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat(new CustomerNumberFormatter(), "{0}: {1}", 
                      customer.CustomerNumber, customer.Name);
      Console.WriteLine(sb.ToString());
   }
}
// The example displays the following output:
//      0000-903-654: A Plus Software

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

The provider parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args. provider can be any of the following:

If the provider parameter is null, format provider information is obtained from the current culture.

args represents the objects to be formatted. Each format item in format is replaced with the string representation of the corresponding object in args. If the format item includes formatString and the corresponding object in args implements the IFormattable interface, then args[index].ToString(formatString, provider) defines the formatting. Otherwise, args[index].ToString() defines the formatting.

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(IFormatProvider, String, Object)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a single argument using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0);
C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, string format, object? arg0);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

format
String

A composite format string.

arg0
Object

The object to format.

Returns

A reference to this instance after the append operation has completed. After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format in which any format specification is replaced by the string representation of arg0.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to one (1).

The length of the expanded string would exceed MaxCapacity.

Examples

The following includes two calls to the AppendFormat(IFormatProvider, String, Object) method. Both use the formatting conventions of the English-United Kingdom (en-GB) culture. The first inserts the string representation of a Decimal value currency in a result string. The second inserts a DateTime value in two places in a result string, the first including only the short date string and the second the short time string.

C#
using System;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder();
      Decimal value = 16.95m;
      CultureInfo enGB = CultureInfo.CreateSpecificCulture("en-GB");
      DateTime dateToday = DateTime.Now;
      sb.AppendFormat(enGB, "Final Price: {0:C2}", value);
      sb.AppendLine();
      sb.AppendFormat(enGB, "Date and Time: {0:d} at {0:t}", dateToday);
      Console.WriteLine(sb.ToString());
   }
}
// The example displays the following output:
//       Final Price: £16.95
//       Date and Time: 01/10/2014 at 10:22

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of arg0 to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. The index of each format item must be zero (0) since this method includes an argument list with a single argument. The formatting process replaces each format item with the string representation of arg0.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. In this case, since the AppendFormat(IFormatProvider, String, Object) method has a single argument in the argument list, the value of index must always be 0. If it is not, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

The provider parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args. provider can be any of the following:

  • A CultureInfo object that provides culture-specific formatting information.

  • A NumberFormatInfo object that provides culture-specific formatting information for arg0 if it is a numeric value.

  • A DateTimeFormatInfo object that provides culture-specific formatting information for arg0 if it is a date and time value.

  • A custom IFormatProvider implementation that provides formatting information for arg0. Typically, such an implementation also implements the ICustomFormatter interface.

If the provider parameter is null, formatting information is obtained from the current culture.

arg0 represents the object to be formatted. Each format item in format is replaced with the string representation of arg0. If the format item includes formatString and arg0 implements the IFormattable interface, then arg0.ToString(formatString, provider) defines the formatting. Otherwise, arg0.ToString() defines the formatting.

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(String, Object[])

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array.

C#
public System.Text.StringBuilder AppendFormat(string format, params object[] args);
C#
public System.Text.StringBuilder AppendFormat(string format, params object?[] args);

Parameters

format
String

A composite format string.

args
Object[]

An array of objects to format.

Returns

A reference to this instance with format appended. Each format item in format is replaced by the string representation of the corresponding object argument.

Exceptions

format or args is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the length of the args array.

The length of the expanded string would exceed MaxCapacity.

Examples

The following example demonstrates the AppendFormat method.

C#
using System;
using System.Text;
using System.Globalization;

class Sample
{
    static StringBuilder sb = new StringBuilder();

    public static void Main()
    {
    int    var1   = 111;
    float  var2   = 2.22F;
    string var3   = "abcd";
    object[] var4 = {3, 4.4, 'X'};

    Console.WriteLine();
    Console.WriteLine("StringBuilder.AppendFormat method:");
    sb.AppendFormat("1) {0}", var1);
    Show(sb);
    sb.AppendFormat("2) {0}, {1}", var1, var2);
    Show(sb);
    sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
    Show(sb);
    sb.AppendFormat("4) {0}, {1}, {2}", var4);
    Show(sb);
    CultureInfo ci = new CultureInfo("es-ES", true);
    sb.AppendFormat(ci, "5) {0}", var2);
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    Console.WriteLine(sbs.ToString());
    sb.Length = 0;
    }
}
/*
This example produces the following results:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

args represents the objects to be formatted. Each format item in format is replaced with the string representation of the corresponding object in args. If the format item includes formatString and the corresponding object in args implements the IFormattable interface, then args[index].ToString(formatString, provider) defines the formatting. Otherwise, args[index].ToString() defines the formatting.

If the string assigned to format is "Thank you for your donation of {0:####} cans of food to our charitable organization." and arg0 is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(String, Object)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a single argument.

C#
public System.Text.StringBuilder AppendFormat(string format, object arg0);
C#
public System.Text.StringBuilder AppendFormat(string format, object? arg0);

Parameters

format
String

A composite format string.

arg0
Object

An object to format.

Returns

A reference to this instance with format appended. Each format item in format is replaced by the string representation of arg0.

Exceptions

format is null.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to 1.

The length of the expanded string would exceed MaxCapacity.

Examples

The following example demonstrates the AppendFormat method.

C#
using System;
using System.Text;
using System.Globalization;

class Sample
{
    static StringBuilder sb = new StringBuilder();

    public static void Main()
    {
    int    var1   = 111;
    float  var2   = 2.22F;
    string var3   = "abcd";
    object[] var4 = {3, 4.4, 'X'};

    Console.WriteLine();
    Console.WriteLine("StringBuilder.AppendFormat method:");
    sb.AppendFormat("1) {0}", var1);
    Show(sb);
    sb.AppendFormat("2) {0}, {1}", var1, var2);
    Show(sb);
    sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
    Show(sb);
    sb.AppendFormat("4) {0}, {1}, {2}", var4);
    Show(sb);
    CultureInfo ci = new CultureInfo("es-ES", true);
    sb.AppendFormat(ci, "5) {0}", var2);
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    Console.WriteLine(sbs.ToString());
    sb.Length = 0;
    }
}
/*
This example produces the following results:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/

Remarks

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. The index of the format items must be 0, to correspond to arg0, the single object in the parameter list of this method. The formatting process replaces each format item with the string representation of arg0.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element Description
index The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.
,length The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatString A standard or custom format string that is supported by the parameter.

Note

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

arg0 represents the object to be formatted. Each format item in format is replaced with the string representation of arg0. If the format item includes formatString and arg0 implements the IFormattable interface, then arg0.ToString(formatString, null) defines the formatting. Otherwise, arg0.ToString() defines the formatting.

If the string assigned to format is "Thank you for your donation of {0:####} cans of food to our charitable organization." and arg0 is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AppendFormat(IFormatProvider, String, ReadOnlySpan<Object>)

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter span using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat(IFormatProvider? provider, string format, scoped ReadOnlySpan<object?> args);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

format
String

A composite format string.

args
ReadOnlySpan<Object>

A span of objects to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format is null.

The length of the expanded string would exceed MaxCapacity.

format is invalid.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the length of the args span.

Applies to

.NET 9
Product Versions
.NET 9

AppendFormat<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat<TArg0,TArg1,TArg2>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1, TArg2 arg2);

Type Parameters

TArg0

The type of the first object to format.

TArg1

The type of the second object to format.

TArg2

The type of the third object to format.

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

arg0
TArg0

The first object to format.

arg1
TArg1

The second object to format.

arg2
TArg2

The third object to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format is null.

The index of a format item is greater than or equal to the number of supplied arguments.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

AppendFormat<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat<TArg0,TArg1>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1);

Type Parameters

TArg0

The type of the first object to format.

TArg1

The type of the second object to format.

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

arg0
TArg0

The first object to format.

arg1
TArg1

The second object to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format is null.

The index of a format item is greater than or equal to the number of supplied arguments.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

AppendFormat<TArg0>(IFormatProvider, CompositeFormat, TArg0)

Source:
StringBuilder.cs
Source:
StringBuilder.cs

Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of any of the arguments using a specified format provider.

C#
public System.Text.StringBuilder AppendFormat<TArg0>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0);

Type Parameters

TArg0

The type of the first object to format.

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

arg0
TArg0

The first object to format.

Returns

A reference to this instance after the append operation has completed.

Exceptions

format is null.

The index of a format item is greater than or equal to the number of supplied arguments.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9