.NET 5 released. So what?

.NET 5 was released a few weeks ago. On the Microsoft website, you can find detailed announcement with all the changes, but the main question for me as a developer is so what? What do I have with the release of the fifth dotnet, how can I use it, does it make sense to switch to the new version right now? I will try to answer these questions further.

image

Performance

The performance changes are the most visible part of the release. Much has been written about them on the Microsoft website and told on .NET Conf… Let’s take a closer look and determine where, how much and due to what productivity increased.

image

Performance measurements from this screenshot are based on benchmarks from TechEmpower. Here Microsoft compares the benchmark results for .NET 3.1 with the expected values ​​for .NET 5. It should be noted that these are the expected values ​​- there have been no real results from TechEmpower yet. However, these results already look impressive – an acceleration of 38 percent for Plaintext, 42 percent for Json, and 20 percent for the Fortunes category (for an explanation of the different types of benchmarks, see TechEmpower Wiki).

In addition to benchmark results from TechEmpower, Microsoft also talks about other improvements. For example, JSON serialization is 20% faster, and serialization of large collections and arrays is up to three times faster.

Also, performance measurements were made by the author of the library Fusion Alexander Yakunin. In two articles on Medium, he first measured, how much faster the work of demos its libraries (20 to 105 percent) and then checked the fifth .NET on several synthetic benchmarks – and also got some speedup.

How is this happening? First of all, thanks to changes in the GC, which led to a decrease in the number of allocations and faster garbage collection. By the way, all these changes are carried out simultaneously with the rewriting of the GC in C #, which means its code becomes much more accessible, understandable and safer. Details can be found in article from Microsoft

Other changes have affected JIT compilation, asynchrony, and many of the base library methods. This applies to methods related to working with strings, regular expressions, and most importantly, collections and serialization. In all the same article there are many examples of such changes, with corresponding pull requests and benchmarks.

The changes to JSON serialization are particularly impressive. Just look at the results of the large array serialization benchmark!

private MemoryStream _stream = new MemoryStream();
private DateTime[] _array = Enumerable.Range(0, 1000).Select(_ => DateTime.UtcNow).ToArray();

[Benchmark]
public Task LargeArray()
{
    _stream.Position = 0;
    return JsonSerializer.SerializeAsync(_stream, _array);
}

image

Short – the fifth dotnet has grown significantly in performance and this is the most important part of the new release. Most standard web applications can expect performance gains of 20% or more, and individual applications can even get several times faster.

Linguistic

The ninth version of C # was released with .NET 5. You can find the official page with changes on the Microsoft website, but let’s try to figure it out – what exactly is worth attention here?

The most notable change is the record types, which allow you to get rid of the boilerplate when writing DTOs. Records are immutable reference types with simple and short declarations. These defaults to the Equals, HashCode, Copy, Clone, PrintMembers, and ToString methods – of course, they all use the values ​​of the type fields to perform operations. That is, Equals correctly compares two records by value, not by reference.

Also, records support the syntax of copying with changing field values ​​via with, for example:

var person = new Person { FirstName = "Mads", LastName = "Nielsen" };
var otherPerson = person with { LastName = "Torgersen" };

You can read more about using records in the blog of Vladimir Khorikov and in publications from Konrad Kokosa

Another important change is the updated pattern matching. Patterns can now be customized by type and comparison operator, and you can combine several different comparisons through logical operators. For instance:

DeliveryTruck t when t.GrossWeightClass switch 
{
    < 3000 => 10.00m - 2.00m,
    >= 3000 and <= 5000 => 10.00m,
    > 5000 => 10.00m + 5.00m
}

The rest of the changes are not so noticeable – they concern high-level programs (the ability to write short programs without a base class and main function), a simplified syntax for new (without specifying a type), init-only setters, and others.

Recent changes in the new C # that I would like to mention are extended support for source generators… This functionality allows you to generate code when compiling a project and is often very convenient for library authors. In the new release, working with code generators has been made a little easier by expanding the functionality of partial classes (some of the old restrictions have been removed from them) and adding module initializers (methods that are called before the first access to any field / method of the module).
You can find a good example of working with code generators in this Medium article

We should also talk about F # 5 separately. The new version of the language has added a lot of cool features for scripts (for example, a directive for connecting nuget packages), slicing for more convenient work with data, and also improved work with quotation expressions and computation expressions. And the interop with C # and performance has also greatly improved. In short, the new release looks interesting and makes F # potentially more useful in production. A full description of all changes can be found on the Microsoft website

Short – the tongue was overgrown with various sugar. The most notable change in C # 9 is the records, but it’s hardly worth updating in and of itself. On the other hand, if you update the Dotnet version for some other reason, then you will have new functionality that makes the language a little more powerful. A trifle, but nice.

Other

In addition to global platform improvements, Microsoft has also done a good job on individual libraries.

For example, in the new version of Dotnet, desktop support has been greatly improved. WinForms and WPF finally got all the advantages of .NET Core – cool performance, packaging the entire application into one .exe, work without a dotnet installed on the machine and … and no, not cross-platform. All this works only under Windows.

Among other things, for desktop development under Windows, a normal visual designer, tooling, new controls and improved support for old ones were also added. In short, if you develop applications using WinForms or WPF, your life will be better.

With cross-platform, everything remains the same. The MAUI release has been ported to .NET 6, and Microsoft does not offer other solutions for the cross-platform desktop yet. So we continue to use Avalonia (or you can turn your attention to Uno Platform – a promising-looking framework for developing everything at once).

There are even more enhancements for full stack web development using Blazor. The main one is performance improvements – Microsoft promises that the WebAssembly version of Blazor will speed up as much as three times. One of the main reasons for this performance increase is the server-side prerender. We also added lazy dependency loading, CSS / JS isolation within the file, new controls and much more. In general, if you were going to try Blazor, but still couldn’t get your hands on it, now it looks like a much more production-ready technology.

image

Of the remaining changes, the most noticeable are those that are designed to help microservices and work with cloud services in general. Here and reducing the size of Docker containers, and improved gRPC support, and OpenAPI support by default, and much more. By the way, Azure has full support for .NET 5 since its release.

And Microsoft rolled out Project tye, which is just a tool for convenient management of microservices. It’s hard to say at the moment whether Tye is worth using in production, but you definitely need to pay attention and play with it in your free time.

Short – Microsoft has improved support for individual tools and added nice features for the cloud. If you are using WinForms, WPF or Blazor, the upgrade is definitely worth the upgrade.

Migration

Let’s see how difficult it will be to migrate your application from .NET Core 3 to .NET 5?

Microsoft Migration Guide tells us that the main difficulties in the migration will concern applications on Blazor, there have been many breaking changes. For most other applications, you just need to update the Dotnet version in application.json, the project file and the dependency versions. Also, the address of the docker container with .NET 5 is slightly different from the corresponding address for .NET 3.1:

docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1
docker pull mcr.microsoft.com/dotnet/aspnet:5.0

Other migration feedback I could find – for example, this guide from Alexandre Malavasi and Fusion migration tweet from Alexander Yakunin – they also say that migration takes place without any specific difficulties. I also did not face any particular problems while migrating my projects and demos to the new Dotnet. In any case, before migrating to a new version, I advise you to carefully study list of breaking changes… Also worth seeing migration report with .NET Conf.

Total

Let’s summarize. Is it worth migrating to a new one and for what?

It seems that the most important thing to bother for is performance. We’ve accelerated almost everything and noticeably enough. Both at a low level – through improvements in GC and JIT – and at the level of individual parts of the framework. So, if you want to win in performance, you should at least try to update the version of the framework and measure how much the main scripts have accelerated.

Other important reasons for migration are improvements for Blazor and WPF / WinForms. If you are using any of these frameworks, it is worth trying to upgrade to .NET 5, but the changes are quite noticeable and useful. However, it should be borne in mind that the migration will be quite difficult for Blazor.

C # 9 is definitely not a major motivation for moving to the new dotnet, but it will bring some nice syntax updates to the company. At the same time, both records and the matching pattern add many opportunities to make the code more confusing – I advise you to discuss this with the team and decide how best to use (or not use) them in the project.

And that is all! Successful migration to the new one will not be. Stay tuned, we will be back with more detailed materials about the modern possibilities of dotnet.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *