IT Pro is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission. Learn more

Microsoft teases 'controversial' changes to C# programming language

The preview features are still in development and have attracted mixed feedback from the developer community

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

Microsoft

Code snippet

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.

Featured Resources

Activation playbook: Deliver data that powers impactful, game-changing campaigns

Bringing together data and technology to drive better business outcomes

Free Download

In unpredictable times, a data strategy is key

Data processes are crucial to guide decisions and drive business growth

Free Download

Achieving resiliency with Everything-as-a-Service (XAAS)

Transforming the enterprise IT landscape

Free Download

What is contextual analytics?

Creating more customer value in HR software applications

Free Download

Recommended

Microsoft launches low-code Power Pages for 'intuitive' web development
web development

Microsoft launches low-code Power Pages for 'intuitive' web development

24 May 2022
Windows 11's nifty new search feature has one major downside
Microsoft Windows

Windows 11's nifty new search feature has one major downside

23 May 2022
Microsoft says it's provided over $100 million in tech support to Ukrainian government
cyber attacks

Microsoft says it's provided over $100 million in tech support to Ukrainian government

20 May 2022
Microsoft to double salary budget to retain workers
Careers & training

Microsoft to double salary budget to retain workers

17 May 2022

Most Popular

Europe's first autonomous petrol station opens in Lisbon
automation

Europe's first autonomous petrol station opens in Lisbon

23 May 2022
Nvidia pauses hiring to help cope with inflation
Careers & training

Nvidia pauses hiring to help cope with inflation

23 May 2022
Open source packages with millions of installs hacked to harvest AWS credentials
hacking

Open source packages with millions of installs hacked to harvest AWS credentials

24 May 2022