Dajbych.net


What’s new in .NET 6?

, 4 minutes to read

net2015 logo

.NET 6 completes the alignment with mobile and desktop development. It introduces the so-called Target framework moniker (TFM), which specifies that the project targets a specific SDK. For example, iOS targeting is written as net6.0-ios. Previously, TFM described a specific version. NET or its clone modified for a specific platform. But that is changing now. All platforms share a single .NET and the specific platform-specific API that is accessible through the SDK is listed after the dash.

HTTP/3

Web developers will be pleased with the experimental support for HTTP/3, because .NET has an implementation of the QUIC protocol included in it. This provides faster connection establishment that does not break if the client’s device has just switched from the Wi-Fi network to the mobile network (or vice versa).

CLI trimming

Common Intermediate Language trimming is a matter known to programmers who are familiar with Xamarin. It is used quite commonly to reduce the size of the resulting application. It includes the entire .NET framework, which makes it difficult when your application is to be regularly updated over mobile data. Therefore, unused parts of the framework (individual .dll files) were omitted in the build. Now it’s going down to the level of individual classes. If you're compiling for a specific instruction set and using this feature, be sure to review the documentation on how to mark pieces of code so that the compiler doesn't miss them.

Priority Queue

It’s a bit strange, but priority queues have only been part of .NET since version 6. You can find it in the familiar namespace:

using System.Collections.Generic;

var queue = new PriorityQueue<TElement,TPriority>();

Periodic timer

In ASP.NET Core apps, there are background tasks that often execute a certain routine at a specific interval. The timer that started the routine periodically had to be linked to CancellationToken in some way. Previously, you had to use a NuGet package for this purpose, or program your own simple timer. However, there is now a PeriodicTimer class that can work with it:

public class TimedHostedService : BackgroundService {

    private readonly ILogger<TimedHostedService> logger;

    public TimedHostedService(ILogger<TimedHostedService> logger) {
        this.logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        logger.LogInformation("Timed Hosted Service running.");
        
        using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
        
        try {
            do {
                ...
            } while (await timer.WaitForNextTickAsync(stoppingToken));
        } catch (OperationCanceledException) {
            logger.LogInformation("Timed Hosted Service is stopping.");
        }
    }

}

What’s new in C# 10?

Interpolated strings

Take, for example, compiling an error message:

var error = "stack overflow"
var message = string.Format("Error: {0}", error);

We can now write the same thing more simply:

var error = "stack overflow"
var message = $"Error: {error}";

Let’s also consider how to write curly braces in such a string:

var str = $"function showMessage(message) {{ window.alert(message); }}";

Top level statements

Until now, every program has been in . NET is run by the Main method in class Program (which did not even have to be static). This is now changing, and the Program.cs file now does not have to contain either a method, a class, or a name file. It might look more like a script:

Console.WriteLine("Hello World!");

Global usings

The program no longer needs to include using System; at the beginning, because C# now has a new declaration of global using. It says that the namespace does not apply only to a specific file, but to the entire project. An important feature is that this declaration is also taken from the SDK used by the project. So, for example, Microsoft.NETCore.App will add to each file in the project:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

You can find out which global usings your project uses in Visual Studio in the project properties in the Global Usings section. You can also add other namespaces there. In this case, it is saved in the project file (.csproj).


Also, be sure to read the traditional summary of performance improvements.