C# 11 is due to be released in November 2022. Instead of waiting until it is officially released, you can start using C# 11 and .NET 7 today! So this begs the question, how do you install the pre-release version of .NET 7 and what cool new features will you be able to play with?

Within this tutorial, not only will you learn how to enable .NET 7 and C# 11 , you will also about the new features that you will most likely use. If you want to learn more about .NET 7, this is the tutorial for you 🔥🔥🔥

In order to try out C# 11 and ASP.NET 7, you will first need to install it. You can download a preview copy of .NET 7 from here.

Unfortunately, simply downloading and running the executable will not magically allow you to create a .NET 7 project, you will also need to enable some settings within Visual Studio as well.

First, you will need to make sure you are using the latest Visual Studio update (as of writing this is 17.4). You can update Visual Studio from within the Help menu. Click on Check Updates. Next, click on Update Settings and select Preview for the update channel. Within the Version available to install section, you should see the latest preview version of Visual Studio. Install it!

Install ASP.NET 7 And Get Started With C#11 Today 1

After upgrading Visual Studio to the latest version and installing .NET 7, you should be able to create a new .NET 7 project:

Install ASP.NET 7 And Get Started With C#11 Today 2

Alternatively, you are also free to target .NET 7 within your .csproj project file, like this:

You are now free to try out .NET 7 🧙🧙🧙

Raw string literals

I think that the feature that you will use the most within .NET 7 will be the new raw string literal feature. One annoying part of creating a string literal is dealing with quotes and single quotes contained within a string. When you have to include either a \ or a " within a string, you will also need to add an escape character, otherwise, your code will not compile. Within C# 11 you can get around this limitation by defining a raw string literal. To define this type, start a string using triple quotes, """...""".

You can also include multi-line and tabs within the raw string. To do this, you need to start and finish the line with """ and then put the text on a different line, like this:

Improved String Interpolation

Asides from raw string, within C# 11 we get improved string interpolation. You can add multiple lines within any text in a non-verbatim interpolation string ($""). You need to add a { on the current line then you are free to add your other commands on an additional line:

The code above would not compile within C# 10 as you were previously not allowed to use newline in non-verbatim string interpolations.

List patterns

One of the more powerful features in .NET 7 is the ability to use pattern searching on enumerable lists and arrays. The pattern matching capabilities within F# have kind of been an inspiration for C#. As a result, pattern match has been continually evolving over the past couple of versions of C#.

List pattern matching will allow you to more easily find items in lists based on some criteria. In order to get started with pattern matching, you need to know about two features, slice and discard.

Slice: Slice can be used to pattern match on zero or more elements. A slice pattern is represented by two dots, ... The slice operator will match every collection entry from the pattern until the next occurrence.

Discard: Discard is the other pattern matching operator. Discard is defined using _. Discard can be used to ignore elements within a pattern. A simple example of how you can use discard is shown below:

A list pattern can be defined with square brackets. You will define 1 to N values that you want to match on. This is handy if you want to check that a collection has a specific set of values. For example the pattern [1, .., 4] matches the value/shape of this array match:

By explicitly defining which values appear in the order of the array, the array will have to match the same length and have the same sequence to return true. Pattern matching can be used with conditional checks like if and switch:

You can also do checks using a switch:

Generic attributes

Historically within C#, you were not allowed to create an attribute with a generic T type. To pass a specific type into an attribute, you would need to pass in the type using a constructor argument and the typeof() method:

Within C# 11, you can now use generic attributes and pass the type in directly in the declaration code:

You can set the generic type to the attribute when it is referenced.

Parameter Null Checking

The feature that I was personally looking forward to the most in C# 11 has been sadly removed. There was the hope of having quick parameter null checking in C# 11, however, in April 2022 this was delayed due to too many objections (read more here).

For those interested, you could have been able to add a guard to a method using !!. If you have not come across the [guard pattern]( https://en.wikipedia.org/wiki/Guard(computerscience)) before, it was simply an argument null check. When writing unit tests, guards are very handy to make sure your class is not instantiated with the wrong dependencies. Instead of writing this code:

You could shorten it like this:

Within C# 11, we do have a new attribute to help at compile time to ensure the correct parameters are set, SetsRequiredMembers. The [SetsRequiredMembers] attribute above the constructor means all properties marked as required must have those members set within the constructor when called:

This could be handy when you are creating base classes and you want to ensure a consumer is setting the correct data:

As you can see, there are definitely a few nice features within C# 11. I do not think any of these features are game changers. They will not revolutionise how you write code with .Net, however, combined they will make your life a little easier. Happy Coding 🤘