0Pricing
C# Academy · Lesson

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

  1. String Interpolation Basics
  2. Composite Formatting with string.Format
  3. Numeric and Date Format Specifiers
  4. Raw String Literals
← Back to C# Academy