Let’s dive into the new features in .NET 8
Microsoft has released the latest version of .NET in November 2023 which is .NET 8 with long term support. What’s interesting about .NET releases is that Microsoft follows a specific release cycle and every year around November a new version is released. The main focus of .NET 8 is to improve parameters such as Performance, .NET MAUI, Cloud native, Artificial Intelligence, ASP.NET Blazor full stack. .NET 8 allows the extensive language models such as OpenAI’s GPT to be now seamlessly integrated with the .NET Application.
To get started you can download the .NET 8 SDK from here or you can use the latest release Visual Studio 2022 here. If you already have Visual Studio 2022, then just upgrade it to Visual Studio 2022 17.8 from the Visual Studio installer itself.
This article focuses on performance-based features that were released in .NET 8 version.
- FrozenDictionary: Immutable collections provide several benefits such as thread safety and consistency, that are useful in building modern software solutions. To handle immutable collections efficiently, .NET 8.0 have introduced FrozenDictionary under the namespace called System.Collections.Frozen.
For instance, you have initialized a dictionary with millions of key-value pairs that are immutable. The querying capabilities of this dictionary can be optimized by changing the regular Dictionary to FrozenDictionary, which is lot more performant.
Below is sample of how FrozenDictionary can be implemented:
Applications requiring concurrent access to data such as finance, healthcare, e-commerce and education data can make use of FrozenDictionary.
- TimeProvider: This feature is a great addition for testing date and time-based logic. Many a times writing test cases for such scenarios could be challenging. TimeProvider is an abstract class that allows developers to override the time component in their applications. The benefit here is that due to its abstract nature the implementation is no longer tightly coupled, making it easy for developers to work with. We can create a custom implementation by inheriting and create our own implementation. We can even create a fake implementation as all the methods are virtual and can be overridden.
For instance, let’s consider the below snippet in which we are using static data DateTimeOffset.UtcNow.
It’s very difficult to write a test case in this scenario, so to overcome this issue in .NET 8 have introduced support for something called as TimeProvider. Instead of using DateTimeOffset.UtcNow directly we are now using the method .GetUtcNow() which can also be customized as needed.
Below is sample of how TimeProvider can be implemented:
As a result with the TimeProvider in .NET 8, its now easy to mock and standardize the behavior of date and time-based functionality. This also results in removal of dependency on third-party tools or custom abstractions, simplifying the development process.
- Random : The Random class was first introduced in .NET 6. In the .NET 8 version a lot of game-changing methods have been introduced that redefine the way randomness is approached. Be it selecting a set of random items from a collection or shuffling of data, its lot easier now.
For instance, we are having are array of elements and we request for 2 random numbers out of this collections using GetItems<T>(). The in-built functionality makes it so easy, unlike the traditional way of looping through collections.
Below is sample of how GetItems<T>() can be implemented:
Similarly, using the Shuffle<T>(), you can easily shuffle the array in a random order. This eliminates the need of custom algorithm implementations and thus making the development process lot simplified.
Along with these interesting performance features, other features include Garbage collector, Native AOT and Code Generation improvements, JSON, Cytrography and Compression enhancements.
More details about this release can be found here.