On the second day of Microsoft Build, C# lead designer Mads Torgersen shed light on the many features planned for the 9.0 release of the general purpose programming language.
A particular focus for C# 9.0 is “supporting terse and immutable representation of data shapes”, Torgersen writes in a blog post. And some improvements to help devs write more concise code come along with it.
The newly introduced init-only properties, for example, can be used to make individual properties immutable in a comparatively easy way. C# 9.0 will also be the first version of the language to allow top level programs – so no need to declare static void Main() for very basic programs anymore.
Back on the immutability front, the new version also sees the introduction of records, which are useful if an object is supposed to act like a value. However, the way it is implemented, records are marked by putting a “data” in front of a class declaration. This led to some mild upheaval amongst potential users, since it doesn’t seem to be the most straightforward naming choice.
Torgersen reasons that the syntax is motivated by a record’s content, which of course is data. Records are meant to represent an object’s state at a given time, not its transformations. To reflect a change of state in a program, a common approach is to copy the old object and modify it accordingly. In C#, this is supposed to be realised by so-called with expressions.
Records “implicitly define a protected ‘copy constructor’ – a constructor that takes an existing record object and copies it field by field to the new one” though that behaviour can be changed according to preference. Using with calls that constructor, and applies an initialiser for property changes.
Other than that, the plan for C# 9.0 is to include better pattern matching, allowing for example relational operators, and combining patterns with logical, spelled out operators. It also comes with improvements for cases in which the type of an expression is taken from its context. More specifically, conditional ?? and ?: expressions won’t fail anymore if the branches have a common target type and new doesn’t need a type specification anymore if it’s clear from the assignment.
The development status of each new language feature, along with information about availability in Visual Studio previews, can be found in the C# repository. And while the milestone view of the project implies a due date in almost 70 years from now, chances are, C# devs won’t have to wait as long for the new version to land. In early May, program manager Richard Lander mentioned in a .NET related post that, “C# will move forward in lock-step with .NET 5” which is supposed to be done in November 2020 and seems a bit more plausible.
C# was initially developed by a team around Anders Hejlsberg, who is also known for his work on Turbo Pascal, and Delphi, as part of Microsoft’s .NET initiative almost 20 years ago. The language is ECMA and ISO approved international standard, intended to be simple, modern, resource economical, and suitable for writing applications for both hosted and embedded systems as well as components in distributed environments.
Today it seems C# can still mostly be found in Microsoft projects such as Visual Studio, as well as a variety of mostly indie games. The latter is mainly down to game engines Unity and Godot offering it as a programming option; more complex titles still tend to get written in C/C++.