What's new with C#11

Just a few days ago we talked about some of the cool new features headed our way via .NET 7 Preview 3. What we did not go into were the improvements coming to C#. Most of the changes greatly boost productivity and code readability. Let's dig into a couple of new features which I feel will have the greatest impact.

Pattern matching

Pattern matching with spans is now possible. Now you can pattern match both Span<char> or ReadonlySpan<char> with a string literal. Kathleen Dollard Principal Program Manager for the .NET gives us a glimpse of what that looks like below:

static bool IsABC(Span<char> s)
{
    return s switch { 
        "ABC" => true, 
        _ => false };
}

Static method groups caching

By caching static method groups rather to constructing new delegate objects, this feature significantly improves runtime speed. You should notice this performance improvement notably in ASP.NET applications without having to make any changes to your code.

Raw string literals

Those working with lengthy and complex strings like JSON, XML, etc. will surely welcome this new feature. Previously, if a literal string was copied with quotes into a C# literal, the string would result in a compiler errors until you escaped each quote. Just as if you pasted text with curly braces into a string literal, each curly brace will be treated as the start of a nested code expression unless you escape it. This new feature frees you from this problem.

raw-string-literal-csharp11

How to get started?

To get started make sure to download the latest preview version of Visual Studio 2022 here and be sure to be on .NET 7 Preview 3.

There are a number of other useful features headed our way with C#11. Sadly some features got dropped too due to negative feedback from the community. You can read more about what's coming and what's being left out by checking the official blog post from Microsoft on the subject here. You can also visit and subscribe to the official CSharpLang GitHub repository to keep up with the latest news. What do you think about the evolution of C#? Do you find these updates and features useful? Give us your thoughts in the comments below.

Share This Post:

Older

Second wave of April Xbox Game Pass games announced, more Ubisoft titles coming soon

LinkedIn loses appeal in data scraping case

Newer