Microsoft teases 'controversial' changes to C# programming language

C# code displayed on a computer screen

Microsoft has offered an early look at the new features coming to the C# programming language as part of the new updates to Visual Studio (Visual Studio 2022 Update 1) and the latest .NET SDK (6.0.200).

All features revealed by Microsoft this week are preview features, meaning they’re in the early stages of development and Microsoft has invited feedback from all corners of the developer community.

Said feedback has already been pouring in since the announcement, with developers specifically vocal on one of the proposed new features: Parameter Null-checking.

The feature aims to introduce a more succinct syntax for checking null values by moving the checking method code to the parameter name, replacing it simply with a !! operator.

Code snippet

(Image credit: Microsoft)

Code snippet

(Image credit: Microsoft)

Example code snippets above show a traditional way of checking for null values against the more succinct preview feature announced this week.

Using the new parameter operator will generate code to perform the null check, said Kathleen Dollard, principal program manager at .NET, and the generated null check will execute before any code in the method body. For constructors, the check performs before the field initialisation, calls to base constructors, and calls to these constructors.

There are a number of restrictions regarding where the syntax can be used in code, interested developers should consult the full release notes for more information.

One individual commenting on Microsoft's blog post announcing the proposed change said the “syntax is ugly as hell. It adds noise to the code”.

It was one of many comments voicing mixed feelings towards the new syntax. Others criticised the new feature for making the code easier to write, but more difficult to read.

There were also suggestions that the existing notnull keyword could be used in place of the !! syntax and enhance compatibility with Nullable Reference Types – a group of features introduced in C# version 8.0 to reduce the likelihood that executed code throws an error.

Replying to commenters, Dollard said: “We’d love any feedback with alternate syntax suggestions”.

Among the other new features is an improvement to string interpolation. Both verbatim and non-verbatim string interpolation styles remain unchanged in their core behaviour, but newlines will now be allowed in the holes of interpolated strings.

“Previously, these restrictions extended to the holes of non-verbatim interpolated strings,” said Dollard. “Holes is a shorthand way of saying interpolation expressions and are the portions inside the curly braces that supply runtime values. The holes themselves are not text, and shouldn’t be held to the escaping/newline rules of the interpolated string text segments.”

The last of the new features is List Patterns, which allow developers to match elements against lists and arrays. Developers can also optionally include a slice pattern that matches zero or more elements.

For example, a pattern such as [1, 2, .., 10] will match any array of integers that contains both a 1 and a 2 at the start of the array and finishes with a 10. These arrays could have any number of additional integers in between those set conditions and still match the pattern.

Connor Jones
News and Analysis Editor

Connor Jones has been at the forefront of global cyber security news coverage for the past few years, breaking developments on major stories such as LockBit’s ransomware attack on Royal Mail International, and many others. He has also made sporadic appearances on the ITPro Podcast discussing topics from home desk setups all the way to hacking systems using prosthetic limbs. He has a master’s degree in Magazine Journalism from the University of Sheffield, and has previously written for the likes of Red Bull Esports and UNILAD tech during his career that started in 2015.