NET Principal Program Manager Kathleen blogged about some of the new preview features of C# 11 that can be experienced in Visual Studio 17.1 and
.NET SDK 6.0.200. Here are some excerpts to introduce some of the new features.
C# 11 Preview: Allow line breaks in “interpolation expressions” for interpolated strings
interpolated strings (interpolated strings) is a syntax introduced in C# 6.0 that allows the insertion of expressions into strings. C#’s interpolated strings are divided into non-word-by-word and word-by-word interpolated strings (
$@"", respectively). The biggest difference between the two is that non-verbatim interpolated strings, like normal strings, cannot contain line breaks in the string text and must use escapes (e.g.
\n). In contrast, a verbatim interpolated string can contain newlines in its text segment and will not escape newlines or other characters.
However, the “newline restriction” in non-literal interpolated strings spreads from the text of the string to interpolated expressions outside the text, which leads to many unnecessary restrictions. For example, before C# 11, the following code would compile with an error.
However, in the C# 11 preview version, it is legal and will compile properly.
A detailed discussion of this new feature can be found in GitHub Issue #4935.
C# 11 Preview: List Mode
The new list pattern allows matching arrays or lists with a range of patterns, for example array is [1, 2, 3] will match an array of integers of length 3 with elements 1, 2, 3. In addition to allowing matching lists and arrays, you can also match elements, and you can choose a slice pattern that contains zero or more elements. Using the slice pattern, zero or more elements can be discarded or captured.
The syntax of a list pattern is a value enclosed in square brackets, and the syntax of a slice pattern is two dots . A slice pattern can be followed by another list pattern, such as the var pattern, to capture the contents of the slice.
For example, the pattern matches all of the following:
[1, 2, . , 10].
To explore list mode, see.
When it passes the following arrays, the result is shown below.
It is also possible to capture the results of slicing patterns.
- List mode applies to any countable and indexable type – this means it has an accessible
Countproperty, and an indexer with an
- Slicing mode applies to any countable and slicable type – this means it has an accessible indexer with
Rangeas a real parameter, or an accessible
Slicemethod with two
- Support for the list mode is currently being considered for addition to the IEnumerable type, click here for a detailed description of this list mode.
C# 11 preview: new parameter null checking
C# 11 provides a simplified syntax for verifying that a method’s argument is not
null and for correctly throwing an
Previously, variants of sample code could be used to verify that method arguments were not null.
Using the new parameter null check, you can automatically perform a null check by adding
! to the parameter name to automatically perform a null check.
The automatically generated null checking code will be executed before the code in the body of the method. For constructors, this null checking occurs before field initialization, calls to the
base constructor, and calls to the
A detailed description of this new parameter null checking can be found here.
How to try the preview feature
To try the preview feature in C# 11, create a C# project with LangVersion set to preview, . The
csproj file is configured as follows.
For more details on C# 11, check out the original blog post or discuss it via the CSharpLang repository on GitHub.