Why You Should Be Using .NET 5 Now to Create Cleaner, Faster Code
.NET, Microsoft’s cross-platform and open source development framework, has become very popular with developers, and with good reason. This blog post will introduce a few of the new features in .NET 5, all of which improve the overall coding experience and make it faster and easier to maintain applications. I’ll also cover some of the improvements in one of the most popular languages used within the framework.
By learning more about .NET 5, we can discover how to use the framework’s powerful tools to create cleaner code that is easier to understand, performs faster, and runs on any platform.
Why .NET 5? What happened to .NET 4?
You’ve probably noticed that Microsoft skipped .NET 4 and dropped Core from the product name. Moving forward, Microsoft plans to unify its entire tooling set under .NET, excluding both Framework and Core naming conventions. This will simplify the adoption of the tools for new developers, and remove unnecessary complications for those trying to understand the current state of the platform.
Microsoft also chose to skip the 4.x versions to minimize potential confusion with all previous versions. If you want to dig deeper, you can read all the documentation here.
What’s new in .NET 5?
Single file deployment
Although this option has been available starting in .NET Core 3.0, there is a very significant Single file deployment that bundles all the application required files (including the runtime and all required framework libraries) into a single file, which greatly simplifies the deployment and distribution of your application. \
Previously, this feature extracted all the files required into a temporary directory before running the application. Now, this step has been eliminated, and single file deployment can run all the code without any need for extraction. This change greatly improves the experience and performance.
To publish your application as a single file, you can use the following CLI command:
Self-contained deployment was introduced with .NET Core, and it meant that you no longer had to have the framework pre-installed on the host for it to run appropriately since the application itself came bundled with everything required. This gave developers more control over exactly which framework version would be used, and also made installation easier. The drawback became the size of the application, which would then contain a full copy of the framework and the runtime.
To tackle this concern, Microsoft introduced application trimming, which scans your code and trims all the unused assemblies. With .NET 5, application trimming adds a new step, opening the assemblies that are being used and removing all types and members that aren’t required.
This new step greatly decreases the overall size of the application. You can also customize it to increase control of what gets trimmed. You’ll find all the details on application trimming here.
JSON features and overall performance improvements in .NET 5
.NET 5 brings many improvements to working with JSON, as well as many performance improvements for the framework itself. In fact, there are too many to cover here, so I will summarize the highlights here.
New JSON features:
- HttpClient and HttpContext now provide extension methods to serialize or deserialize JSON payloads from the network.
- You can deserialize an immutable type or a struct using the [JsonConstruct] attribute on a constructor.
- With the usage of JsonOptions object or specific attribute allows you to ignore object properties based on several conditions, including ignoring all ReadOnly properties or properties that are null.
Performance improvements for .NET 5
- The garbage collection process provides better performance when deallocating unused memory.
- The speed of the Regex engine used has been increased up to 6x times throughput, which makes working with the Regex expression much less painful.
- The overall size of Docker images generated was decreased by as much as 16% for .NET containers, which greatly improved download times when working with those containers.
C#9 features in .NET 5
Top-level calls allow developers to skip a lot of ceremonial code required on the entry point of their console applications. For .NET, this is done by completely omitting the Main method statement. If done appropriately, the compiler will look for a single file in your application that contains immediate code to be run as the defacto entry point. Only one file in your solution can contain top-level calls; otherwise, an error will be returned at compile time.
This means that your Program.cs file can be changed from this:
Init setters give developers new syntax that allows properties of an object to have limited options when setting the value of said property. The init accessor is used instead of a set, which means that those properties can get populated on creation expressions, but after the construction of the object is completed, those properties will behave as read-only.
Records are a compact and simple way to define a reference type that is immutable by default. Record types also permit different behavior when comparing because equality is checked based on the values of the record instead of object identity as is the case for regular classes.
Immutable reference types are safer when passing in concurrent threads or programs that share data. They help minimize the risk of messing up your information. Once created, the values of a Record behave as read-only properties.
Records have some other interesting functionality that makes them handy:
- Value-based equality
- Improved ToString method
- Ability to deconstruct method into a tuple
- Copy and clone method
Upgrading to .NET 5
You can upgrade older versions by targeting this new framework version from the apps and servers where the apps are hosted. Know that there are some very specific functionalities that could break when you upgrade, as some features have become obsolete or modified to follow industry compliance. You can read all of the detailed upgrade information here.
It’s time to get to know .NET 5
Although I’ve covered just a few of the many great features included in .NET 5, I hope you’ll start to see how it can help you to greatly improve the overall quality of code and the overall application development lifecycle, enabling faster and easier deployment processes with less risk of errors.
If you and your team work either as mobile developers using Xamarin or are more used to web development with ASP.NET, everything is now under a single source and faster than ever.
As Microsoft’s .NET framework continues to evolve and gain popularity, I expect that .NET will become one of the leading tools for rapidly developing any kind of application.