What’s New in Microsoft .NET 6?

.NET 6 is now released, bringing a new long-term stable version of .NET to the market. Now, the newer .NET 6 replaces .NET 5, largely viewed as a “skip version” by most developers, with minimal usage in contrast to dot NET Core 3.1. Microsoft’s plan to provide a uniform platform for developing applications for any […]

by Kosta Georgiev

February 15, 2022

5 min read

net 6 whats new - What's New in Microsoft .NET 6?

.NET 6 is now released, bringing a new long-term stable version of .NET to the market. Now, the newer .NET 6 replaces .NET 5, largely viewed as a “skip version” by most developers, with minimal usage in contrast to dot NET Core 3.1.

Microsoft’s plan to provide a uniform platform for developing applications for any device and operating system gains traction with the release of .NET 6. With this ambition in mind, .NET 6 introduces a slew of new capabilities aimed at making development easier and improving performance. 

Unified and Extended Platform

The objective of a single platform for the .NET world began to take shape with the release of .NET 5. That version was planned to be the unifying successor to all of the various .NET flavours, e.g. Framework, Standard, Core, Mono, and so on. .NET 5 also aimed to be a development platform to create any sort of application for any platform. However, at the time, an important piece of the puzzle was missing: a unified framework for designing cross-platform user interfaces. Thanks to .NET MAUI, or the .NET Multi-platform App UI framework, the riddle has been solved. Despite the fact that .NET MAUI is still in preview at the time of writing (February 2022), it is ready to be included in the .NET 6 release.

Nowadays with .NET 6, you can develop any application and launch it practically anywhere: on a PC, a mobile device, the web, or the cloud.

.net development, namespace, .net framework, microsoft .net, .net development platform
Source: Microsoft

The new .NET MAUI framework, in particular, allows you to design desktop and mobile native user interfaces from the same codebase.

Simplified Development with .NET 6

The main goal of the .NET 6 release is to provide features to developers that simplify their work. 

Below, I am going to explain most of them, however, the most exciting contexts are: new C# features, Hot Reload support, and minimal web APIs.

The new C#10 Features:

So .Net 6 comes with C#10 which bring you significant new features that will help you and make your code prettier, faster and a bit more expressive. 

Let’s take a quick look at some of them:

We’ll start with global and implicit using because no C# developer avoids writing using directives in hundreds of files. This is undoubtedly infuriating, but thanks to .NET 6 and C# 10, we have an easy remedy. Consider the System namespace as an example; it’s a really typical scenario, and you’re probably forgetting about it all the time.

As seen in the example, you can declare namespace using a global keyword before utilising it.

global using System;

When you mark System; as global, you no longer have to include the using System; declaration in all files. This namespace will be available throughout the project.

Another method to get that feature (which I believe is a good practice) is to create a file called GlobalUsing.cs and specify your namespace there.

There is a third way to use implicit using by configuring the <ImplicitUsings> element in your .csproj file

.net development, namespace, .net framework, microsoft .net

As a result, we enable the compiler to decide and automatically add global using directives based on the type of your project.

File Scoped Namespace Syntax

The majority of the code in a normal C# source file is indented because it is contained within a namespace declaration. It’s been like that for so long that we no longer notice it, but it’s a waste of horizontal space! C# 10 solves the issue by allowing you to specify a namespace for the entire file without the use of braces. By deleting a nesting level, you tidy up the code. This new syntax, however, does not allow for the creation of nested namespaces.

.net development, namespace, .net framework, microsoft .net

Lambda Improvements

Currently, you must declare the target type of the expression, as demonstrated in the example:

Func<int, bool> odd =  n => (n % 2) == 1;

You may write the above code in C#10 more “naturally,” and the compiler will try to guess the type of the lambda expression for us.

var  odd = (int n) => (n % 2) == 1;

Record Struct

The initial introduction of the record keyword for classes was in C#9. This keyword is applied to reference types that include built-in capabilities for data encapsulation. Records are primarily meant to enable immutable data models.

public record UserProfile(string AccountName, string Email);

C#10 takes records a step further. Currently, you can use structure types to build data-centric types that have the same values but little or no behaviour. In C# 10 and later, record struct types can be defined as follows:

public readonly record struct UserProfile(string AccountName,string Email);

Hot Reload

The Hot Reload update allows you to alter your app’s managed source code while it is running, removing the need to manually pause or hit breakpoints. Simply make a change while the app is running and then click the apply code changes button. It includes the most recent release of Visual Studio 2019.

.net development, namespace, .net framework, microsoft .net, hot reload, hot reload update .net 6

Minimal Web APIs

The minimal web APIs are a fantastic new feature introduced by .NET 6 alongside new top-level statements. With just a few lines of code, you can have a fully functional web API application. Please, take a look at the example below.

//Program.cs

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet(“/”, () => “Hello World!”);

app.Run();

If you’ve worked with web APIs in ASP.NET Core previously, you’re surely aware that there are numerous configurations to make before executing your web API. On the other hand, with the minimal API, everything is straightforward; there is a single file that does not require any further configurations.

This is a wonderful example of simplicity!

.NET 6 Performance Improvements

The .NET team has been working intensively in recent years enhancing the overall platform speed in terms of compilation and execution time, as well as memory use. Developers can see these enhancements in various places of the framework with .NET 6. For example, the endeavour to increase inner-loop efficiency in .NET project development has yielded fantastic results, as shown in the diagram below:

.net development, namespace, .net framework, microsoft .net, comparison .net 5 vs .net 6, dot net framework, .net performance
Source: Microsoft

The IO subsystem is another area with performance improvements. For instance, the FileStream class was almost totally redesigned with outstanding results even when compared to.NET 5, as summarised in the table below for the writing operation:

filestream performance improvement - What's New in Microsoft .NET 6?
Source: Microsoft

For further information, see a thorough report on speed enhancements in.NET 6 with accompanying benchmarks.

Final Words 

Since .NET Core 1.0, .NET 6 has been the most foundational release. It provides support for significant new hardware platforms, broader use of source generation. Despite discussing many key new features, this article couldn’t touch on yet another performance gain, and dozens of other enhancements. .NET 6 is a reminder that Microsoft is investing in .NET for the long term, across client and cloud platforms. If you’re building cloud or client apps or both, .NET offers a multitude of useful options. Looking ahead, it looks that what follows next will be even more hopeful. It’s a fantastic time to be a .NET developer, as it always has been.

If you’re looking for a technological partner to assist you with custom .NET development, you’ve come to the right place. We at Dreamix have gained significant industry and hands-on experience and would be happy to help you with your next .NET project.

Book free consultation and a Dreamix representative will contact you directly.

Categories

Middle Software Engineer 2 at Dreamix