Why you should upgrade to .NET 7

Squeeds Julkalender | 2022-12-20 | Ali Abdulhussein
Should everyone upgrade to .NET 7 or is it better to wait for .NET 8?
dotnet7.png

.NET 7 launched with more than 1000+ improvements. Here are some of them.

Performance

With performance in mind .NET 7 come with the following improvement in performance:

  • On-stack replacement (OSR) is a complement to tiered compilation. It allows the runtime to change the code executed by a currently running method in the middle of its execution (that is, while it's "on stack"). Long-running methods can switch to more optimized versions mid-execution.
  • Profile-guided optimization (PGO) now works with OSR and is easier to enable (by adding <TieredPGO>true</TieredPGO> to your project file). PGO can also instrument and optimize additional things, such as delegates.
  • Improved code generation for Arm64.
  • Performance improvements to the Mono runtime, which powers Blazor WebAssembly, Android, and iOS apps.
  • Native AOT produces a standalone executable in the target platform's file format with no external dependencies.

 

P/Invoke source generation

.NET 7 introduces a source generator for platform invokes (P/Invokes) in C#. The source generator improves application performance and also allows the app to be ahead-of-time (AOT) compiled.

 

System.Text.Json serialization

Improving System.Text.Json serialization in the following areas:

  • Support for required members, which are properties that must be present in the JSON payload for deserialization to succeed
  • Contract customization gives you more control over how types are serialized and deserialized.
  • Polymorphic serialization for user-defined type hierarchies.

 

.NET SDK

The .NET 7 SDK improves the CLI template experience, enables publishing to containers, and central package management with NuGet.

  • Publish to a container:Container images are now a supported output type of the .NET SDK, and you can create containerized versions of your applications using dotnet publish.
  • Templates: -- prefix no longer include in install, uninstall, search, list, and update subcommands. ex: dotnet new --list become dotnet list
  • Authoring: Template constraints, a new concept for .NET 7, let you define the context in which your templates are allowed.
  • Central package management: You can now manage common dependencies in your projects from one location using NuGet's central package management (CPM) feature.
  • Manage JSON Web Tokens in development: dotnet user-jwts command line tool can create and manage app specific local JSON Web Token (JWTs)
  • Trim mode: when publishing the application, the .NET SDK analyzes the entire application and removes all unused code. In .NET 7 trim mode is full by default.

 

Regular expressions

RegEx are faster by enhancing RegexOptions:

  • RegexOptions.IgnoreCase no longer calls ToLower on each character in the pattern and on each character in the input. Instead, all casing-related work is done when the Regex is constructed.
  • The new option RegexOptions.NonBacktracking enables matching using an approach that avoids backtracking and guarantees linear-time processing in the length of the input (right-to-left search not supported).

 

Observability

Improving observability in .NET 7 as follow:

  • The new Activity.CurrentChanged event, which you can use to detect when the span context of a managed thread changes.
  • New, performant enumerator methods for Activity properties: EnumerateTagObjects(), EnumerateLinks(), and EnumerateEvents().

 

.NET libraries

Many improvements have been made to .NET library APIs, like:

  • APIs for reading, writing, archiving, and extracting Tar archives
  • Support for microseconds and nanoseconds in TimeSpan, TimeOnly, DateTime, and DateTimeOffset types
  • New type converters for DateOnly, TimeOnly, Int128, UInt128, and Half

 

Dotnet 7 is an STS version - Standard Term Support, while Dotnet 6 still is LTS - Long Term Support. The next release of .NET should be as an LTS (.NET 8).

Answer to previous question should we upgrade to .NET 7 or not: It depends on how often and how fast you can move to new version, how many dependencies and how much your organization like to run the last version of .NET.

Read more (What's new in .NET 7)