New in .NET 8 runtime

What's new in .NET 8 runtime

.NET 8 is the successor to .NET 7. It will be supported for three years as a long-term support (LTS) release

LTS (Long Term Support)

The abbreviation stands for Long Term Support. Long Term Support means that a release labeled LTS is maintained for an extended period over regular releases.

.NET runtime The .NET 8 runtime includes improvements to performance, garbage collection, and the core and extension libraries. It also includes a new globalization mode for mobile apps and new source generators for COM interop and configuration binding.

Garbage collection

Garbage collection in C# automatically manages memory by reclaiming unused objects, preventing memory leaks and manual memory management errors. It runs in the background, identifying and freeing up memory occupied by objects that are no longer referenced by the program. This process involves marking objects for deletion and then compacting the memory to optimize usage. Developers benefit from improved memory efficiency and reduced risk of bugs related to memory management, allowing them to focus more on application logic rather than memory allocation and deallocation.

.NET 8 introduces the ability to adjust the memory limit on the fly. This is useful in cloud-service scenarios where demand varies. To be cost-effective, services should scale their resource consumption up and down as demand changes. When a service detects a decrease in demand, it can reduce its memory limit to scale down resource consumption. Previously, this would fail because the garbage collector (GC) was unaware of the change and might allocate more memory than the new limit. With this update, you can call the RefreshMemoryLimit() API to inform the GC of the new memory limit.

The following code snippet shows how to call the API.

GC.RefreshMemoryLimit();

You can also refresh some of the GC configuration settings related to the memory limit. The following code snippet sets the heap hard limit to 100 mebibytes (MiB):

AppContext.SetData("GCHeapHardLimit", (ulong)100  1_024  1_024); 
GC.RefreshMemoryLimit();

Extension libraries

Extension libraries in .NET add extra features beyond the standard .NET framework. These libraries, often available as NuGet packages, enhance .NET by providing reusable components, controls, utilities, and APIs. Developers can easily integrate them into their projects using tools like NuGet Package Manager. This enriches applications with features such as advanced data processing, UI components, logging frameworks, and more. Extension libraries boost productivity by cutting down development time with pre-built solutions and offering specialized tools for specific tasks or industries. This creates a strong ecosystem of community-contributed and commercially-supported extensions.

Globalization

Globalization in .NET refers to designing applications that can adapt to various cultural and regional preferences. This includes formats for dates, times, currencies, and languages. The globalization mode in .NET enables developers to create applications that are locale-aware and can dynamically adjust to user preferences based on the current culture settings. This mode utilizes features such as resource files for different languages, culture-specific formatting classes, and the ability to handle multilingual content seamlessly. By supporting globalization, .NET applications can cater to diverse audiences worldwide, providing a localized user experience that enhances usability and accessibility across different languages and regions.

Globalization for mobile apps Mobile apps on iOS, tvOS, and MacCatalyst can opt into a new hybrid globalization mode that uses a lighter ICU bundle. In hybrid mode, globalization data is partially pulled from the ICU bundle and partially from calls into Native APIs. Hybrid mode serves all the locales supported by mobile.

Hybrid mode is most suitable for apps that can't work in invariant globalization mode and that use cultures that were trimmed from ICU data on mobile. You can also use it when you want to load a smaller ICU data file.

Source generators for COM

Source generators for COM interop in .NET automate the generation of interop code necessary to communicate with COM (Component Object Model) components. They analyze COM type libraries at compile-time and produce C# source code that wraps the COM components, eliminating the need for developers to manually write tedious and error-prone interop code. This approach improves efficiency and reduces maintenance overhead by ensuring that the interop code is always up-to-date with the COM interfaces and types. Source generators for COM interop streamline integration of COM components into .NET applications, facilitating seamless interoperability and enhancing developer productivity in cross-platform scenarios.

.NET 8 includes a new source generator that supports interoperating with COM interfaces. You can use the GeneratedComInterfaceAttribute to mark an interface as a COM interface for the source generator. The source generator will then generate code to enable calling from C# code to unmanaged code. It also generates code to enable calling from unmanaged code into C#.

Configuration binding

Configuration binding in .NET refers to the process of mapping application settings defined in configuration files (like app.config or web.config) to .NET objects. This mapping is facilitated by the .NET configuration system, which uses configuration sections and settings to define how various aspects of the application behave or are configured. During runtime, .NET reads these configuration settings and binds them to corresponding properties of .NET objects, allowing developers to access and use these settings programmatically. Configuration binding simplifies application configuration management, promotes maintainability, and enables flexible customization of application behavior without modifying the source code.

.NET 8 introduces a source generator to provide AOT and trim-friendly configuration in ASP.NET Core. The generator is an alternative to the pre-existing reflection-based implementation.

The source generator probes for Configure(TOptions), Bind, and Get calls to retrieve type info from. When the generator is enabled in a project, the compiler implicitly chooses generated methods over the pre-existing reflection-based framework implementations.

No source code changes are needed to use the generator. It's enabled by default in AOT-compiled web apps, and when PublishTrimmed is set to true (.NET 8+ apps). For other project types, the source generator is off by default, but you can opt in by setting the EnableConfigurationBindingGenerator property to true in your project file