Composite Formatting with string.Format
Use indexed placeholders and alignment.
Composite Formatting with string.Format is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
Composite Formatting
string.Format uses numbered placeholders like {0} and {1} that map to the arguments that follow.
using System;
class Program {
static void Main() {
string msg = string.Format("Hello, {0}!", "Ada");
Console.WriteLine(msg);
}
}Multiple Arguments
Each index refers to an argument by position. {0} is the first, {1} the second, and so on.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("{0} + {1} = {2}", 2, 3, 5));
}
}Reusing an Index
You can use the same index more than once to repeat an argument without passing it twice.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("{0}{0}{0}", "ab"));
}
}Format Specifier After Colon
Add a colon and a specifier to control formatting, such as {0:F2} for two decimals.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("Pi is {0:F2}", 3.14159));
}
}Alignment with Comma
A comma and width set alignment: {0,8} right-aligns in eight characters; a negative width left-aligns.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("[{0,8}]", "hi"));
Console.WriteLine(string.Format("[{0,-8}]", "hi"));
}
}Alignment and Format Together
You can combine both: {0,10:F2} formats to two decimals and right-aligns in ten characters.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("[{0,10:F2}]", 3.5));
}
}Console.WriteLine Formatting
Console.WriteLine accepts the same composite format string and arguments directly.
using System;
class Program {
static void Main() {
Console.WriteLine("{0} scored {1} points", "Sam", 42);
}
}Building a Table
Alignment makes simple aligned tables easy in console output.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("{0,-10}{1,5}", "Apples", 3));
Console.WriteLine(string.Format("{0,-10}{1,5}", "Pears", 12));
}
}Interpolation Compiles to This
Behind the scenes, interpolated strings often compile to composite formatting. They are two faces of the same feature.
using System;
class Program {
static void Main() {
int n = 5;
Console.WriteLine(string.Format("n = {0}", n));
Console.WriteLine($"n = {n}");
}
}Escaping Braces
As with interpolation, double a brace to print it literally in a composite format string.
using System;
class Program {
static void Main() {
Console.WriteLine(string.Format("{{0}} shows literally, {0} shows value", 7));
}
}When to Choose Format
Composite formatting is handy when the template is stored separately, such as in resource files for localization.
using System;
class Program {
static void Main() {
string template = "Welcome, {0}";
Console.WriteLine(string.Format(template, "Lee"));
}
}Quick Check
Test your knowledge of composite formatting.
Recap
string.Format uses indexed placeholders ({0}, {1}) mapped to arguments by position, which can be reused. A colon adds format specifiers and a comma sets alignment width.
It powers Console.WriteLine formatting and underlies interpolation, and it is ideal when templates live in separate resources.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Composite Formatting with string.Format” lesson free?
Yes — the full text of “Composite Formatting with string.Format” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “Composite Formatting with string.Format”?
Use indexed placeholders and alignment. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Composite Formatting with string.Format” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String Interpolation Basics
- Composite Formatting with string.Format
- Numeric and Date Format Specifiers
- Raw String Literals