DEV Community

Salisu Adeboye
Salisu Adeboye

Posted on

How to Use Raw String Literals for Cleaner, More Readable Code in C# 11

As software developers, we’re always on the lookout for ways to write cleaner, more maintainable code. With every new version of C# and .NET, Microsoft introduces features that help us do just that. One of the most underrated yet powerful features introduced in C# 11 is Raw String Literals — a game-changer for handling multi-line strings, JSON, SQL queries, and more.

In this post, I’ll show you how to leverage raw string literals in your C# projects to write code that’s not only easier to read but also less error-prone. Plus, I’ll share a neat trick to dynamically embed variables inside raw strings without losing readability.

What Are Raw String Literals?
Traditionally, multi-line strings in C# have been a bit cumbersome. You had to use escape sequences (\n, \”, etc.) or string concatenation, which often made your code messy and hard to maintain.

Raw string literals solve this by allowing you to write multi-line strings exactly as they appear, without needing to escape characters.

Syntax Example:

string json = """
{
 "name": "John Doe",
 "age": 30,
 "isDeveloper": true
}
""";
Enter fullscreen mode Exit fullscreen mode

Notice how clean and readable that is? No escape characters, no concatenation, just pure text.

*Why Should You Care?
*

  • Improved readability: Your strings look exactly like the output you want.
  • Less error-prone: No more missing escape characters or broken formatting.
  • Great for embedded code: Perfect for SQL queries, JSON, XML, HTML, or any other structured text.

*The Hack: Embedding Variables in Raw String Literals
*

One limitation of raw string literals is that they don’t support string interpolation directly. But here’s a neat trick to get around that using string.Format or the new FormattableString API.

Example: Using string.Format with Raw Strings

string userName = "Alice";
int userAge = 28;
string messageTemplate = """
Hello, {0}!
You are {1} years old.
""";
string message = string.Format(messageTemplate, userName, userAge);
Console.WriteLine(message);
Enter fullscreen mode Exit fullscreen mode

Output:
Hello, Alice!
You are 28 years old.

This way, you keep the raw string’s readability while still injecting dynamic content.

Bonus: Using FormattableString for More Flexibility

If you want to keep the interpolation syntax but still use raw strings, you can combine raw strings with FormattableString:

string userName = "Alice";
int userAge = 28;
FormattableString message = $"""
Hello, {userName}!
You are {userAge} years old.
""";
string result = message.ToString();
Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

This approach lets you write interpolated raw strings cleanly.

When to Use This?

  • Writing complex SQL queries inside your C# code.
  • Embedding JSON or XML payloads for APIs.
  • Creating multi-line HTML templates.
  • Any scenario where string readability and maintainability matter.

Bonus Tip: How to Detect Raw String Literals in Your Codebase
If you want to find places in your code where you can replace old multi-line strings with raw string literals, try this simple Visual Studio search pattern:

@”[^”]*”

Look for verbatim strings (@””) that span multiple lines and consider refactoring them to raw string literals for better clarity.

I hope you find this tip useful! Follow me for more practical .NET and C# tricks that make your coding life easier.

Happy coding! 🚀

Top comments (0)