When is it better to use String.Format vs string concatenation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, you have three main ways to build strings: concatenation with +, String.Format(), and string interpolation ($""). String concatenation is simplest for combining 2-3 values. String.Format is better for localization and reusable format templates. String interpolation (C# 6+) combines readability with performance and is the preferred choice for most modern C# code. Performance differences are negligible in most cases — readability and maintainability should drive the decision.
String Concatenation (+ Operator)
The compiler optimizes adjacent string concatenations into a single String.Concat() call:
When Concatenation Works Well
When Concatenation Becomes Unreadable
String.Format()
Format Specifiers
String.Format supports rich formatting that concatenation cannot do inline:
Localization
String.Format works with resource files for localization:
Concatenation cannot do this because the argument order is baked into the code.
String Interpolation ($"") — Preferred in Modern C#
C# 10+ Improvements
Performance Comparison
When to Use Each
| Scenario | Best Choice | Why |
| Simple 2-3 value join | Concatenation or interpolation | Readable, no overhead |
| Formatted numbers/dates | Interpolation with format specifiers | $"{price:C}" is clean |
| Localized messages | String.Format with resource templates | Template from resource file |
| Reusable format template | String.Format | Template stored as variable |
| Building strings in a loop | StringBuilder | Avoids O(n²) allocation |
| Logging (C# 10+) | Interpolation | Handler avoids allocation if log level disabled |
| SQL/HTML templates | Neither — use parameterized queries/templates | Prevents injection |
Common Pitfalls
- Concatenation in loops: Each
+=creates a new string object and copies all previous characters. This is O(n²) for n iterations. UseStringBuilderfor any loop that builds a string incrementally. - Wrong placeholder index in
String.Format:String.Format("{0} {2}", a, b)throwsFormatExceptionat runtime because{2}requires a third argument. String interpolation avoids this entirely since values are inline. - Using string building for SQL/HTML: Never build SQL queries or HTML with concatenation,
String.Format, or interpolation. This creates injection vulnerabilities. Use parameterized queries for SQL and template engines for HTML. - Premature optimization: The performance difference between concatenation,
String.Format, and interpolation for a few values is nanoseconds. Choose based on readability. Only optimize string building in hot loops or when profiling shows it matters. - Forgetting
StringBuilder.ToString():StringBuilderdoes not implicitly convert tostring. You must call.ToString()to get the final string. Passing aStringBuilderwhere astringis expected causes a compile error or callsObject.ToString()which gives the type name.
Summary
- Use string interpolation (
$"") as the default in modern C# — readable and performant - Use
String.Formatwhen the format template comes from a resource file (localization) or is stored as a variable - Use concatenation only for trivially simple joins of 2-3 values
- Use
StringBuilderfor building strings in loops — never concatenate with+=in a loop - Format specifiers (
{0:C},{0:N2},{0:yyyy-MM-dd}) work in bothString.Formatand interpolation - Performance is almost never the deciding factor — choose for readability
Related reading
- When is too much async and await? Should all methods return Task?
- When is Windows Forms the correct choice vs WPF?
- When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?
- When should copy-local be set to true and when should it not?
- When should I use a struct instead of a class?
- When should I use async controllers in ASP.NET MVC?
- When should I use GC.SuppressFinalize?
- When should I use LazyT?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.