In this article, you will learn about the various new features of .NET 6.
On 8th November, 2021, Microsoft announced a new version of .NET called .NET 6. According to Microsoft, it the "fastest .NET yet" and this version has LTS (long time support version) of 3 years.
Basically, .NET 6 is more focused on the C# and F# languages and they have introduced C# 10 and F# 6 with this release, which will make our code simpler, faster and better than older versions of .NET. This version is available for the Linux, Mac and Windows platforms.
Let's drive through the features of this release of .NET version.
This is the main and noticeable change in these features. .NET 6 has worked on a lot of functionalities to make them better, but we will cover three of them now.
In .NET 6,
System.IO.FileStream namespaces and all its classes are almost re-written. That makes this class faster and more reliable than the previous one.
Now, file stream will not block asynchronous I/O operations on Windows. There are few examples.
In .NET 5, the
getlength() method from
fileStream namespace takes 1,932 ns to execute whereas in .NET 6, it takes only 58.52 ns to execute. So as you can imagine, it is now multiple times faster. The same thing can be experienced on Mac and Linux environments.
.NET 6 has made performance-related changes in JIT compilation technique. Basically, JIT works on Profile Guided Optimization (PGO) technique where it optimized the compile code, but in .NET 6, they have introduced Dynamic Profile Guided Optimization (DPGO), in which optimized code is generated by the JIT compiler. However, this time it works with the tired compilation to more optimized code based on tier 0. DPGO is disabled by default - you need to enable it to use this feature.
.NET 6 has introduced Crossgen 2, that helps to generate and optimize native code in a faster way. Crossgen 2 is re-written in C# instead of C++.
With this improved version of Crossgen, the analysis and optimization of native code is possible, so it boosts up the startup time of the application.
For those who don't know about Crossgen, here is a small introduction.
What is Crossgen?
Crossgen is a tool that provides an earlier compilation of code before the actual JIT works, so the JIT time can be reduced. Basically it runs above all assemblies and stores the compiled code in a separate section. Then, that compiled code is fetched quickly at runtime. It helps in faster startup for application.
Crossgen2 was introduced to come over the limitation of Crossgen, which was only able to produce native code of one thing at a time.
.NET 6 now supports ARM64 based processors and operating systems in Mac and Windows. This means from now, it supports both X64 ARM and 64-bit OS.
For those who don't know about ARM OS, here is a small introduction.
Basically, ARM is a different CPU architecture like X86(32 bit) and X64(64 bit) for Windows, but this architecture was developed for mobile and smart devices.
ARM64 is the next version of ARM architecture that supports 64-bit processing.
This feature is very useful for developers. They can modify and instantly apply those changes to your running application. It will help them to increase the development process, basically this feature helps to reduce the time between number of edits and application compilation. All application development types are supported by this feature like web, desktop, mobile development, cloud and other project types. But make a note that this feature will not be supported in Xamarin forms and applications that build using F#.
The Hot Reload icon is shown in Visual Studio using a fire like symbol. This feature not only helps to apply code changes instantly, but alsohelps to restart application faster.
Additionally, this feature also introduces Hot Reload on Save functionality where, upon clicking the Save button, the application will automatically hot reload and apply saved changes. It is important to note that this feature is optional and we can turn it off at any time.
MAUI is also known as .NET multi-platform app UI. This feature has just been introduced and is in the preview stage. With this feature, developers can use a single code base and developed application (client apps) for desktop and mobile. .NET MAUI uses native container and controls for Blazor hybrid scenarios.
C#10 and Its Related Features
With .NET 6, C# 10 and its related features are also included. In these .NET features, C# has been modified and upgraded to use some new language features like:
- sync main method
- target typed new expression
- file scoped named space
- Nullable reference type
Here, one thing to be note is that your existing C# code or functionality will not hamper after upgrading to .NET 6.
The following new improvements can be found in C#:
- native .NET task support
- indexing syntax made more simpler with expr[idx]
- "as" pattern
- revision on indentation syntax
- binary number format
- changes in NativePtr
HTTP/3 is the new version of HTTP. In this new version, HTTP uses QUIC protocol, which is used as a underlaying connection protocol.
This protocol helps in establishing a connection more quickly and independent of IP address. It also allows mobile and smart devices to roam between WIFI and other wireless networks, before this version http is using HTTP/1.1 and HTTP/2.
HTTP/3 improved a connection request even when there is small amount network packet.
System.Text.Json API Improvements
There are lot of improvements done in JSON API, some of them are as follows:
New Source Generator
.NET 6 introduced a new source generator for
System.Text. JSON, now source generator is working with JSON sterilizer and can be configured in multiple ways. It helps to speed up the performance, reduce memory consumption.
**For who those don't know about source generator (SG), it is a C# compiler feature that generates new C# source files on the fly that are added to the compilation.
This is the new lightweight serialization alternative when the use of a plain CLR object is not possible. It helps us to navigate through large JSON tree and read a node.
The following types are supported by new serialized writeable DOM:
Use of IAsyncEnumerable in JSON
The serialization and deserialization of
IAsyncEnumerable <T> is now possible with .NET 6.
IAsyncEnumerable types can be serialized as a JSON array and for deserialization, a new method is used (
Below are the newly added serialization interfaces:
.NET 6 has also put some improvements in security: it now comes with Control-flow Enforcement Technology and write exclusive execute.
Write exclusive execute(W^X)
This security aspect blocks simple attack paths by avoiding writeable and executable memory pages at a time.
Control-flow Enforcement Technology (CET)
Basically, CET (control flow technology) is introduced by Intel and it helps us to protect from control flow hijacking attacks. .NET 6 also provides support for x64 apps.
Unused Assemblies Trimming
Basically, this functionality is already introduced in .NET 5 where it helps to trim unused assemblies, but in .NET 6 the functionality is upgraded to unused types and members too. Moving forward, unused types and members are also trimmed.
Improvements for Windows Form
Application.SetDefaultFont(Font) is new method introduced by .NET 6, as the name suggests, it sets the default font to the win-form.
In addition to this, C# Windows Form been updated to give support to using directives, file scoped namespace, and nullable reference.
New Target Framework
New target framework has been introduced in .NET 6. Now you can see net 6.0-android, net 6.0-ios, and net 6.0-macos in target framework.
New API Related to LINQ
.NET 6 has released new LINQ related API like:
Enumerable.TryGetNonEnumeratedCount - get number of elements in a sequence
Enumerable.Chunk - break the sequence elements into chunks
Enumerable.MaxBy - find max element using key
Enumerable.MinBy - find min element using key
.NET compatibilities and supported versions are as follows:
- .NET 6 is supported on VS 2022 that is Version 17
.NET 6 is released with the following runtime support:
- .NET Runtime 6.0.0
- ASP.NET Core Runtime 6.0.0
- .NET Desktop Runtime 6.0.0
- .NET 6 does support the following languages:
- C# 10.0
- F# 6.0
- Visual Basic 16.9
Points of Interest
If your application was build on legacy .NET framework, then it will take more effort to migrate it to 6.x.
I hope this article will give you an idea about what's new in .NET 6. In the next article, we will see more about C# 10.
Suggestions and queries are always welcome.
Thanks for reading!
- 30th May, 2022: Initial version