News
Microsoft Quickens C# Release Cadence, Unveils v7.1
In keeping with the general trend of releasing software versions more frequently, Microsoft is quickening the release cadence of its C# programming language, starting with the new C# 7.1.
As many .NET coders already know, C# 7.1 was actually released in August, but Microsoft yesterday chose to highlight the new version and review its new features in a post announcing the accelerated release schedule.
Mads Torgersen, lead designer for C#, said the team is moving to "point releases" to keep up with associated tooling -- like Visual Studio -- and the underlying .NET Framework itself that are shipped more frequently. This, he said, brings new features to developers in a trickle so they can take advantage of them sooner. With Visual Studio 2017 (update 15.3 is required for the new C# 7.1 functionality), developers can configure the IDE to incorporate minor x.x releases or stick only to major x.0 versions.
In C# 7.1, three new (admittedly minor) features are being called out by Microsoft for special attention in the release notes:
- async Main method
The traditional Main method that starts C# projects can now include the async modifier, which lets coders use the await keyword (actually introduced in C# 5.0). Converting Main into an async method lets it return Task or Task(int), causing a program to wait for the Task to execute.
That, Torgersen said, does away with the need to roll your own helper method and lets developers turn this code:
static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();
into:
static async Task Main(string[] args)
-
Default literal expressions
Developers can use default literal expressions in default value expressions in those cases when the target type can be inferred. This enhancement means developers no longer need to specifically indicate the type on the right-hand side of an expression initialization, turning code like this:
Func<string, bool> whereClause = default(Func<string, bool>);
into:
Func<string, bool> whereClause = default;
-
Inferred tuple element names
This feature just streamlines the initialization of tuples, themselves only recently supported in C#. Noting that "many times when you initialize a tuple, the variables used for the right side of the assignment are the same as the names you'd like for the tuple elements," Microsoft said the feature can turn code like this:
int count = 5;
string label = "Colors used in the map";
var pair = (count: count, label: label);
into:
int count = 5;
string label = "Colors used in the map";
var pair = (count, label); // element names are "count" and "label"
While these features can save developers some coding time and ease repetitive typing, one programmer commenting on Torgersen's post objected to the injection of more implicit features -- often called "syntactic sugar." The reader said:
Please ease up on implied constructs such as the inferred tuple element names. These new implicit features will only increase ramp-up times and make things more complicated.
Yes, you can just opt not to use them, but trying to stop people from using language features is a lost cause. Too much sugar can make you sick, and likewise too much syntactic sugar can make a codebase very unhealthy to read.
Here is Torgersen's response to that:
Indeed, too much sugar can make you sick. We try to add it where we have high confidence that it will make your code better. In this case, we knew that the shorthand would be useful -- we've already had it for anonymous types since C# 3.0.
In general, the language has to grow with usage. A sharp corner that people have to turn all the time? We should probably round it! We have to not be afraid to make common tasks easier. But at the same time I completely agree with you that there are dangers of making code to cryptic, with too many things being too implicit. It's a tradeoff.
Torgersen said his team is already working on C# 7.2 as well as the next major release, inviting developers to follow the progress on the project's C# Language Design GitHub site.
About the Author
David Ramel is an editor and writer at Converge 360.