The articles Twelve C# 11 Features by Oleg Kyrylchuk and Welcome to C# 11 by Mads Torgersen (Microsoft .NET Blog) provide an excellent overview with examples of new features in C# 11, available with .NET 7.0.
I include my own notes below.
“Obvious” to me, at least. The terms link to examples in one of the articles linked above.
u8
to the end of a literal string to make it UTF-8 instead of the system-standard UTF-16. For example, “Test string”u8
will be encoded by the compiler as UTF-8 and will have the type ReadOnlySpan<byte>
.Finally, you can just pass a formatted and indented JSON into C# code, interpolate some variables, and do it all without escaping anything![1]
“In fact .NET 7 comes with a new namespaceSystem.Numerics
chock-full of math interfaces, representing the different combinations of operators and other static members that you’d ever want to use. […] All the numeric types in .NET now implement these new interfaces – and you can add them for your own types too! So it’s now easy to write numeric algorithms once and for all – abstracted from the concrete types they work on – instead of having forests of overloads containing essentially the same code.”
See here for an example of using generic parameters in operators
, or Generic Math for an example that uses some of the new interfaces, like IAdditionOperators
and ISubtractionOperators
.
In that vein, there are a lot more interfaces that support generalized computation, like ISpanParsable<TSelf> Interface, which “[d]efines a mechanism for parsing a span of characters to a value.”
“Another ongoing theme that we’ve been working on for several releases is improving object creation and initialization. C# 11 continues these improvements with required members.”
[Generic<MyType>]
declared an attribute of type GenericAttribute
parametrized with MyType
.nameof
Scopenameof
with “method parameter[s] in an attribute on the method or parameter declaration.”StringSyntaxAttribute
RegEx
or DateTime.Format
), this is a welcome standardization that gives your own APIs the same star treatment. The post What does the StringSyntaxAttribute do? includes a list of the syntaxes supported out-of-the-box. The post StringSyntaxAttribute for syntax highlighting provides examples and screenshots.A few that seem a bit dubious, but are, I guess, welcome additions, and will be useful to someone are,
numbers is [_, >= 2, _, _]
returns true
if numbers
is a four-element list where the second element is greater than or equal to 2.System.Numerics
interfaces and the increased generality offered by abstracting over static members (linked above).This feature leverages the source-generation that’s been available since .NET 5 to avoid JIT for regular expressions by generating code for it directly. It’s really great to see the .NET team getting mileage out of the features they’re adding (I’m sure this isn’t a coincidence).
For another example of source-generation, see Generating PInvoke code for Win32 apis using a Source Generator by Gérald Barré (Meziantou's Blog), which explains how to use Microsoft’s NuGet package Microsoft.Windows.CsWin32
to easily generate source for any Win32 API or type—no more writing this stuff manually!
Check out the following animation of converting an escaped string to a raw string in Rider (from the post Rider 2022.3: Support for .NET 7 SDK, the Latest From C#11, Major Performance Improvements, and More! by Sasha Ivanova (The .NET Tools Blog):
↩