Multiline String Interpolation in .NET 7
In .NET 7 we can now use multiline in string Interpolation statements
Before writing the code, I would like to explain what string interpolation is.
String interpolation is the $ sign in front of the string that allows us to put C# code in between the string with the help of curly braces.
Step 1:
Open Visual Studio 2022. For this demonstration, I’m using Visual Studio 2022, version 17.4.1; if you want.NET 7 support in Visual Studio, use this version or higher.
Select Create a new project.
Step 2
Search Console App
Select Console App (make sure you choose the Console App not Console App .NET Framework as it is a older version of .NET)
Click “Next.”
Step 3
Provide the name in the project name field.
Click “Next.”
Step 4
In this step, you have to select the framework.
Select .NET 7.0
Click “Next.”
Step 5
Lets now write some code
Before .NET 7 we were writing string interpolation in the following way where we have some logic and we show the values in the string with the help of curly braces which is the standard way of writing string interpolation
// Standard Way to write String Interpolation
int marks = 67;
string securedClass = marks switch
{
>= 70 => "Distinction",
>= 60 => "First",
>= 50 => "Second",
>= 40 => "Pass",
_ => "Fail"
};
Console.WriteLine($"Student got {marks} marks and secured {securedClass} class");
In this example I have declared marks variable and from marks we are finding out the class that the student earned with the help of switch expression
So I got the marks and the secured class text, and I created a securedClass variable simply to put it inside a string interpolation.
In.NET 7, we can now put multiple lines inside our curly braces in string interpolation, which means I can paste the above-mentioned switch expression in place of the securedClass variable in the string interpolation.
Let me demonstrate you with the code
// New Way to String Interpolation in .NET 7
int marks = 67;
Console.WriteLine($"Student got {marks} marks and secured {marks switch
{
>= 70 => "Distinction",
>= 60 => "First",
>= 50 => "Second",
>= 40 => "Pass",
_ => "Fail"
}} class");
And now we have a switch expression inside the string interpolation and it works just fine
Let me run this program to show you the output
As you can see, the output is as expected; the student got 67 and secured First Class, which is good.
With that, we now have multiline interpolation statements that allow it to give it a little bit more stuff inside of those curly braces. It allows us to put more complex c# code in there.
I am not encouraging you to use it all the time, as it may make your code messy. So use it wisely so that your code remains clean and readable.
So this is the new feature in C# 11. That might be a small thing, but it gives you another option, and it is good to have an option. Options allow us to do things differently.
If you like this article also have a look @ .NET 7 Overview article where you can find the Changes and Support Cycle of it.
Thanks for reading the article. Please let me know your thoughts about this feature. Do let me know where you are going to use this feature in the comment section or mail me @ [email protected].