Dajbych.net


What’s new in .NET 8?

, 2 minutes to read

net2015 logo

.NET 8 brings another number of innovations. One of them is the ability to compile a ASP.NET Core application in AOT (ahead-of-time) mode. In this regime, however, forget about MVC, Blazor or SignalR right away. On the other hand, you will be rewarded with an application that will be smaller, take up less memory and start significantly faster. In addition, .NET 8 also brings the ability to easily generate random strings. And C# is not on the sidelines either, as its 12th version fundamentally simplifies class notation.

Frozen collections

.NET includes new type collections

You may wonder why this is the case, when .NET already contains

The answer lies in the way they are used. Frozen collections are optimized for read speed. It is suitable, for example, for configuring an application that is created only at its startup and values are often read from it. However, creating such a collection is more time-consuming than in the case of immutable collections.

RandomNumberGenerator.GetString

Generating random tokens is straightforward in .NET 8:

var token = RandomNumberGenerator.GetString("abcdefghijklmnoqrstuvwxyz0123456789", 32);

The nice thing about this is that you can directly define what characters should appear in the token.

What’s new in C# 12?

Primary constructors

Often, the parameters of a class constructor were just copied into its body to be later available to its methods. Now, constructor parameters are available to all methods throughout the lifetime of the class.

class Car(string brand, Color color) {
    public string Brand => brand;
    public Color color => color;
}

Collection expressions

Whenever we need to create a collection, we can use the abbreviated [] similar to JavaScript. This new syntax also combines well with the ability to create new instances of a class without specifying its type in places where the type is known in advance.

List<Car> cars = [
    new("Jaguar", Color.DarkGreen), 
    new("Land Rover", Color.SandyBrown), 
    new("Toyota", Color.Silver)
];

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