Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion _rules/1546.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ rule_category: maintainability
title: Prefer interpolated strings over concatenation or `string.Format`.
severity: 1
---
Since .NET 6, interpolated strings are optimized at compile-time, which inlines constants and reduces memory allocations due to boxing and string copying.
Since .NET 6, interpolated strings are optimized at compile-time, which inlines constants and reduces memory allocations due to boxing and string copying. In .NET 8 and later, interpolated strings are further optimized using `DefaultInterpolatedStringHandler`, making them the fastest option in most scenarios.

// GOOD
string result = $"Welcome, {firstName} {lastName}!";
Expand All @@ -17,3 +17,5 @@ Since .NET 6, interpolated strings are optimized at compile-time, which inlines

// BAD
string result = string.Concat("Welcome, ", firstName, " ", lastName, "!");

For building strings in loops or with many parts, consider using `StringBuilder` or raw string literals (C# 11+) when appropriate.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would using raw string literals help in loops?