C# 11 Preview Features: Multiline Interpolated Strings, List Patterns, and Parameter Null Checking
The article introduces three C# 11 preview features—allowing line breaks inside interpolated string expressions, a new list pattern syntax for matching arrays and slices, and a concise parameter null‑checking syntax—explaining their usage, showing code examples, and describing how to enable them in a project.
The .NET chief project manager Catherine posted a blog outlining several preview features of C# 11 that can be tried in Visual Studio 17.1 and .NET SDK 6.0.200. The article extracts three notable additions.
C# 11 Preview: Multiline Interpolated String Expressions
Interpolated strings, introduced in C# 6.0, allow embedding expressions inside a string using "$"" (non‑verbatim) or "$@"" (verbatim). Previously, non‑verbatim interpolated strings could not contain line breaks, requiring escape sequences like \r or \n . In C# 11, line breaks are permitted inside the expression part of an interpolated string, removing this limitation. The following code, which previously caused a compilation error, now compiles:
var v = $"Count is\t: { this.Is.A.Really()
.That.I.Should(
be + able)[
to.Wrap()] }.";Reference: GitHub Issue #4935.
C# 11 Preview: List Patterns
List patterns enable matching arrays or lists against a sequence of patterns, e.g., array is [1, 2, 3] matches an integer array of length 3 with those exact elements. Slice patterns ( .. ) allow discarding or capturing zero or more elements. Examples:
int[] arr1 = { 1, 2, 10 };
int[] arr2 = { 1, 2, 5, 10 };
int[] arr3 = { 1, 2, 5, 6, 7, 8, 9, 10 };A switch expression using list patterns:
public static int CheckSwitch(int[] values) => values switch
{
[1, 2, .., 10] => 1,
[1, 2] => 2,
[1, _] => 3,
[1, ..] => 4,
[..] => 50,
};Calling the switch with different arrays yields the corresponding results (1, 2, 3, 4, or 50). Slice patterns can also be captured:
public static string CaptureSlice(int[] values) => values switch
{
[1, .. var middle, _] => $"Middle {String.Join(", ", middle)}",
[.. var all] => $"All {String.Join(", ", all)}",
};List patterns apply to any type with a Length or Count property and an indexer taking int or System.Index . Slice patterns require a type supporting Range or an indexer with two int parameters. Support for IEnumerable is under consideration.
C# 11 Preview: New Parameter Null‑Checking
C# 11 adds a concise syntax to validate that a method parameter is not null and automatically throw an ArgumentNullException . By appending !! to a parameter name, the compiler injects the null‑check before the method body (or before base/this constructor calls).
public static void M(string s!!)
{
// Body of the method
}The equivalent pre‑C# 11 code required an explicit if (s is null) throw new ArgumentNullException(nameof(s)); block.
How to Try the Preview Features
Create a C# project and set LangVersion to preview in the .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>For more details, refer to the original blog or the discussions in the CSharpLang GitHub repository.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.