C# String Format() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, String.Format() builds a new string from a format template and values. Placeholders such as {0} and {1} are replaced by the arguments.

You pass the format string and the values. The format string is required. Currency and dates follow culture rules.

Syntax

Pass the format template, then the values. Start with this call.

C#
string s = string.Format("Hello, {0}!", name);
CallWhat it does
string.Format("Hello, {0}!", name)Builds Hello, name! with the value of name.

This is a static method. Escape a literal brace with {{ or }}.

Library declarations
C#
public static string Format(string format, object arg0);
public static string Format(string format, params object[] args);

Parameters

ParameterDescription
formatRequired. The template with placeholders such as {0}.
argsRequired. The values that fill the placeholders.

Return Value

Return ValueDescription
New stringThe formatted text.

C# Examples

Example 1 — Basic Placeholders

Insert a name and a score into a template using {0} and {1}.

C#
using System;

class Program {
    static void Main() {
        string name = "Ada";
        int score = 95;

        string message = string.Format("Hi {0}, score {1}", name, score);

        Console.WriteLine(message);
    }
}

How It Works

  1. Write a template. "Hi {0}, score {1}" marks two insertion points.
  2. Pass arguments in order. First value fills {0}, second fills {1}.
  3. Get a new string. Result is "Hi Ada, score 95".
  4. Inputs unchanged. name and score stay the same.

Example 2 — Number Format Specifiers

Control how numbers look with :F2 (fixed decimals) and :N0 (grouped whole number).

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        double pi = 3.14159;
        int big = 1000000;

        string avg = string.Format(CultureInfo.InvariantCulture, "Avg: {0:F2}", pi);
        string count = string.Format(CultureInfo.InvariantCulture, "Count: {0:N0}", big);

        Console.WriteLine(avg);
        Console.WriteLine(count);
    }
}

How It Works

  1. Add a specifier. {0:F2} means fixed-point with 2 decimals.
  2. Use InvariantCulture. Keeps . and , predictable in samples and logs.
  3. N0 groups thousands. 1000000 becomes 1,000,000 under invariant rules.
  4. Currency tip. {0:C} follows the current (or provided) culture—great for UI, less stable for demos.

Example 3 — Format vs Interpolation

Same message two ways: string.Format and a $"..." interpolated string.

C#
using System;

class Program {
    static void Main() {
        string product = "Keyboard";
        int price = 50;

        string withFormat = string.Format("{0} costs {1}", product, price);
        string withInterp = $"{product} costs {price}";

        Console.WriteLine(withFormat);
        Console.WriteLine(withInterp);
        Console.WriteLine($"Same text? {withFormat == withInterp}");
    }
}

How It Works

  1. Format uses indexes. {0} and {1} map to arguments by position.
  2. Interpolation names values. $"{product}" embeds expressions directly—often easier to read.
  3. Same result here. Both produce "Keyboard costs 50".
  4. When Format wins. Templates from files, resources, or variables still need string.Format.

Frequently Asked Questions

Format() builds a new string from a template and values using placeholders like {0}.
format is required. args are the values that fill the placeholders.
A new formatted string.
FormatException is thrown.
Write {{ for a left brace and }} for a right brace.

Next: GetEnumerator()

Learn how to iterate a string character by character.

GetEnumerator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful