New Features In .NET 7

Introduction

Microsoft has just released .NET 7 on 14th November 2022. In this and the upcoming articles, we will look into some new features that have been introduced with C# in the latest version of .NET. Let us begin.

Improved way to handle string literals

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

New features in .NET 7

New features in .NET 7

Now, add the below code to the Program.cs file:

// Declare a string literal with quotes

//.NET 6
var firstValue = "<?xml version = \"1.0\"?>";

//.NET 6
var secondValue = @"<?xml version = ""1.0""?>";

//.NET 7
var thirdValue = """<?xml version = "1.0"?>""";

Console.WriteLine(firstValue);
Console.WriteLine(secondValue);
Console.WriteLine(thirdValue);

Run the application and you will see the below output,

New features in .NET 7

Here, you see that the string literal with quotes has been displayed on the console three times. The first two methods were available in .NET 6 and earlier. These required some modifications to the text itself. However, in .NET 7 you do not need to make any updates within the string itself. Simply, adding three quotes makes the string useable. If your string contains three quotes, just change the prefix to four quotes. This simplifies the handling of XML and JSON type strings.

Summary

In this article, we looked at a new feature that has been introduced with .NET 7. This makes JSON and XML string literals more readable and easier to understand in the code. In the next articles, I will cover more features introduced with .NET 7.


Similar Articles