C# 11 coming soon: Generic math, required class members, raw string literals

break point

Mads Torgersen, lead designer of C#, showed off some of the highlights of C# 11, which will be released with .NET 7.0 in November, at the .NET Conf earlier this month.

Torgersen picked out three big features of the forthcoming release: generic maths, required class members, and raw string literals.

C# raw string literals demonstrated by lead designer Mads Torgersen

“We ship every year now,” said Torgersen – though DevClass notes that .NET 7.0 will be a Current release, supported for only 18 months. The next long-term support (LTS) release will be .NET 8.0 expected in November 2023.

Much of Torgersen’s talk was devoted to describing the intricacies of static abstract members in interfaces, described in the docs as the ability to “define interfaces that include overloadable operators, other static members, and static properties … the primary scenario for this feature is to use mathematical operators in generic types.”

Torgersen went straight for this primary scenario in presenting the feature, which now allows functions that accept and return generic types while still performing operations such as addition and subtraction.

While a neat feature, we should note that there is some unhappiness in the community about its implementation, thanks mainly to a restriction that was introduced “disallowing interface as type arguments where the constraint contains an interface which has static virtual members.”

One developer commented as a result: “I don’t really see how either static abstract or static virtual in interfaces are even remotely useful for much of anything.”

That said, developers have welcomed generic math in C# as shown by comments to this detailed post, saying for example “Generic Math and Int128 are things I’ve daydreamed about for at least a decade.”

Another key update is required members. Adding the “required” modifier to properties or fields means that they must be initialized in the constructor. Among other things, this simplifies inheritance.

Perhaps the biggest change for day-to-day developers though is the introduction of raw string literals. “Raw string literals remove the need to ever use escape sequences. You can write the string, including whitespace formatting, how you want it to appear in output,” say the docs.

The way to define a raw string literal is to start and end with a sequence of at least three double quotes. Why “at least”? The reason, as Torgersen explained, is to allow multiple double quotes in the raw string literal itself. In the event that a sequence of three double quotes appears within the string, coders can use four double quotes as the delimiters, and so on.

Similar logic applies to string interpolations, where text within braces is replaced by output at runtime. Interpolated strings are prefixed with the $ character, and “if your output string should contain the { or } character, you can use extra $ characters to specify how many { and } characters start and end an interpolation.”

A nice feature of raw string literals is control over whether white space in the editor is included or excluded from the literal. “What really determines how this white space is sliced off is where this end quote is placed,” said Torgersen. Visual Studio assists by drawing a faint vertical line showing what is and is not part of the string literal.

Raw string literals are not a ground-breaking feature, but they are easily understood and useful, which will be welcome to C# developers.