C# › Fundamentals

How does string interpolation work, and how does it compare to string.Format() and simple concatenation?

Intermediate

Answer

String interpolation (`$"..."`) embeds expressions directly inside `{}` within a string literal, compiled internally into either a string.Format() call or (in modern .NET) a more efficient interpolated string handler — it's generally more readable than string.Format()'s positional placeholders and avoids the performance pitfalls of repeated '+' concatenation in a loop.
string name = "Sam";
int age = 30;
string message = $"{name} is {age} years old."; // readable, compiler-optimized

// Equivalent, more verbose alternatives:
string message2 = string.Format("{0} is {1} years old.", name, age);
string message3 = name + " is " + age + " years old.";
Real-world example Building a readable, formatted log message or user-facing display string combining multiple variable values.

Common follow-ups: Why is repeated string concatenation with '+' inside a LOOP specifically discouraged, in favor of StringBuilder?