What's new in .NET 8?
Tags: dotnet, csharp, news, discuss
Published: 2023-11-15 06:56:07

.NET 8 has been released, so it's time to start porting your projects to the new version. In this article, we'll look at new features and enhancements: C# 12, performance, Native AOT, GC, new types aimed at improving performance, NuGet Audit, and more.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0subimpch6tzpy1askl3.png)

## C# 12
We have already covered the new features of C# 12 in this [article](https://pvs-studio.com/en/blog/posts/csharp/1074/). There, we discussed new language features: primary constructors, default parameters, collection expressions, inline arrays, and so on. There aren't many major changes this time. What do you think about the update? We've looked at it and immediately got some ideas for new C# analyzer rules.

By the way, since we're talking about the analyzer, I'd like to make a small announcement. We are already working on the .NET 8 and C# 12 support. We will introduce it in PVS-Studio 7.28. The release is scheduled for early December. If you don't want to miss it, I invite you to sign up for the [press-release newsletter](https://pvs-studio.com/en/subscribe/).

## Performance
Microsoft said that .NET 7, introduced last year, is very fast, but .NET 8 is faster. And that's true. Stephen Toub, in his typical fashion, told us about performance improvements in .NET 8 in the [article](https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/). It's several hundred pages long and covers most (if not all) of the improvements. The following things have been enhanced: JIT, GC, reflection, collections, LINQ, code generation, serialization and deserialization, primitive data types, and much more.

The article is focused on comparing the performance of .NET 7 and .NET 8 — there are lots of comparison tables. I think everyone will find something interesting there. So, you may want to take some time off to read it.

## Native AOT
Just a quick reminder of what the technology is: in short, Native AOT uses an ahead of time compiler to compile IL into machine code while publishing a [self-contained](https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained) application. .NET 8 adds support for x64 and arm64 architectures on macOS.

Native AOT applications include a .NET runtime. As a result, they are larger than regular applications. .NET 8 has enhanced this aspect as well. The table below shows the size of the "Hello World" program for both .NET 7 and .NET 8:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95y6de7pzs68fcocrgzu.png)

As you can see, the size of the application on Linux has been reduced by 50%.

Moreover, .NET 8 is starting to enable native AOT support for platforms such as iOS-like platforms. Note that this is just the beginning of the work. So, the developers ask you not to jump to conclusions about performance. You can now build and run .NET iOS and .NET MAUI applications with Native AOT on the following platforms: ios, iossimulator, maccatalyst, tvos, and tvossimulator.

## New types aimed at improving performance
.NET 8 introduces new types aimed at improving app performance in different scenarios. Developers now have the FrozenDictionary<TKey, TValue> and FrozenSet<T> collections, which are in the System.Collections.Frozen namespace. They prohibit changes to keys and values once a collection has been created. This enables them to be optimized specifically for read operations. This is a very handy feature for when a collection is filled in for the first time and stored for a long time. Here's an example of how to use the collection:

```
private static readonly FrozenDictionary<string, bool> _settings = 
  GetSettings().ToFrozenDictionary();
....
if (_settings.TryGetValue(key, out bool setting))
{
    //....
}
```
A new [SearchValues<T>](https://learn.microsoft.com/en-us/dotnet/api/system.buffers.searchvalues-1) type has been introduced. It provides an immutable, read-only set of values optimized for efficient searching.

Another new type, [CompositeFormat](https://learn.microsoft.com/en-us/dotnet/api/system.text.compositeformat), has been introduced specifically for cases where format strings are unknown at compile time. For example, when loading a format string from resources.

Finally, there are new [XxHash3](https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing.xxhash3) and [XxHash128](https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing.xxhash128) types that implement the fast hashing algorithms — XXH3 and XXH128.

## NuGet Audit
Security plays an important role in the development process, and .NET developers always remember that. Now, when running dotnet add and dotnet restore, you will get warnings about every package that contains a vulnerability.

By the way, PVS-Studio can search for vulnerable components used in your project. If the library you are using contains a vulnerable library, the analyzer will issue a warning. The analyzer searches not only for direct but also for transitive dependencies. To learn more about [PVS-Studio as an SCA solution](https://pvs-studio.com/en/pvs-studio/sca/), please follow the link.

## Random
New methods for handling randomness have been added:

- [Random.GetItems](https://learn.microsoft.com/en-us/dotnet/api/system.random)
- [RandomNumberGenerator.GetItems](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator)
- [Random.Shuffle](https://learn.microsoft.com/en-us/dotnet/api/system.random.shuffle)
- [RandomNumberGenerator.Shuffle](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator.getitems)

The GetItems method helps randomly select a specified number of items from the passed set. The Shuffle method shuffles the passed sequence of items.

As the developers say, such features will come in handy in the field of machine learning.

## Garbage collection
Starting with .NET 8, you can regulate memory limits. This feature may be useful for cloud services. For example, you can reduce the amount of memory available when the load is low. To adjust the limits, call the [RefreshMemoryLimit method](https://learn.microsoft.com/en-us/dotnet/api/system.gc.refreshmemorylimit#system-gc-refreshmemorylimit).

You can now also update some GC configuration settings. For example, it's possible to set a hard limit for a heap size:

```
AppContext.SetData("GCHeapHardLimit", _memoryLimit);
GC.RefreshMemoryLimit();
```

## System.Text.Json enhancements
With the release of the new .NET, serialization and deserialization have gotten a lot better. There are a lot of enhancements, and I'll highlight the most important ones.

So, here they are:

- improved performance;
- reduced the size of Native AOT applications that use System.Text.Json;
- added serialization support for required and init members;
- added interface hierarchy support;
- extended the [JsonSourceGenerationOptionsAttribute](https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonsourcegenerationoptionsattribute) functionality;
- added the option to disable reflection-based serialization, which is enabled by default.

You may read the following article to learn more about new features, bug fixes, and more: ["What's new in System.Text.Json in .NET 8"](https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-8/).

## Conclusion
Looking back at the features added in .NET 7, not much seems new in .NET 8.

However, I can assure you that there are still plenty of enhancements. It seems that this time the developers focused more on targeted improvements to the platform. There are a lot of not-so-big but still important enhancements in many areas of .NET usage.

The article lists only the most interesting features that are likely to be useful to the majority of developers. You can read about all the improvements [here](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8). If you find something useful that isn't mentioned in the article, feel free to write about it in the comments.

Are you already using some of the new .NET features? Let me know in the comments!

**More interesting articles you can find in our [PVS-Studio blog](https://pvs-studio.com/en/blog/posts/)**