Microsoft teases 'controversial' changes to C# programming language
The preview features are still in development and have attracted mixed feedback from the developer community
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.
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.
The state of Salesforce: Future of business
Three articles that look forward into the changing state of Salesforce and the future of businessFree Download
The mighty struggle to migrate SAP to the cloud may be over
A simplified and unified approach to delivering Enterprise Transformation in the cloudFree Download
The business value of the transformative mainframe
Modernising on the mainframeFree Download
The Total Economic Impact™ Of IBM FlashSystem
Cost savings and business benefits enabled by FlashSystemFree Download