Using Multi-Line String Literals In .NET 7

Introduction

Microsoft has just released .NET 7 on 14th November 2022. In the previous articles, we looked at some of the new features.

Today, we will look at another feature introduced with .NET 7 and that is the new multi-line string literals. Let us begin.

The multi-line string literal

Let us create a console application using Visual Studio 2022 Community edition.

Using Multi-line String Literals in .NET 7

Using Multi-line String Literals in .NET 7

Now, add the below code to the “Program.cs” file,

var firstName = "John";
var lastName = "Smith";
Console.WriteLine($"This is a testing string literal with {firstName} and {lastName}");

When we run the application, we see the below,

Using Multi-line String Literals in .NET 7

Hence, in .NET 7/ C#11, we can break the string literal into multiple lines in order to improve readability. However, when it is displayed it will show on one line.

Summary

In this article, we looked at a new feature that has been introduced with .NET 7. Using the multi-line feature we can make our string literals more readable. In the next article, we will look into another feature of .NET 7.