Composite Formatting with string.Format
Use indexed placeholders and alignment.
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));
}
}All lessons in this course
- String Interpolation Basics
- Composite Formatting with string.Format
- Numeric and Date Format Specifiers
- Raw String Literals