
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.
string s = string.Format("Hello, {0}!", name);
| Call | What 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
public static string Format(string format, object arg0);
public static string Format(string format, params object[] args);
Parameters
| Parameter | Description |
|---|
| format | Required. The template with placeholders such as {0}. |
| args | Required. The values that fill the placeholders. |
Return Value
| Return Value | Description |
|---|
| New string | The formatted text. |
C# Examples
Example 1 — Basic Placeholders
Insert a name and a score into a template using {0} and {1}.
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
- Write a template.
"Hi {0}, score {1}" marks two insertion points. - Pass arguments in order. First value fills
{0}, second fills {1}. - Get a new string. Result is
"Hi Ada, score 95". - 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).
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);
}
}
Avg: 3.14
Count: 1,000,000
How It Works
- Add a specifier.
{0:F2} means fixed-point with 2 decimals. - Use InvariantCulture. Keeps
. and , predictable in samples and logs. - N0 groups thousands.
1000000 becomes 1,000,000 under invariant rules. - 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.
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}");
}
}
Keyboard costs 50
Keyboard costs 50
Same text? True
How It Works
- Format uses indexes.
{0} and {1} map to arguments by position. - Interpolation names values.
$"{product}" embeds expressions directly—often easier to read. - Same result here. Both produce
"Keyboard costs 50". - 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.
About the author
Developer, cloud engineer, and technical writer
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