.NET 9 and C# 13: New Features and Improvements
This article explores significant updates and additions announced during MS Build 2024 providing developers with a comprehensive overview of what to expect.
Join the DZone community and get the full member experience.
Join For FreeThe development of the .NET platform and C# language moves forward with the launch of .NET 9 and C# 13, introducing a range of enhancements and advancements to boost developer efficiency, speed, and safety. This article delves into upgrades and new features in these releases giving developers a detailed look.
Figure courtesy of Microsoft
.NET 9
.NET 9 introduces a range of improvements, to the .NET ecosystem with a strong focus on AI and building cloud-native distributed applications by releasing .NET Aspire, boosting performance and enhancements to .NET libraries and frameworks. Here are some notable highlights:
.NET Aspire
It's an opinionated stack that helps in developing .NET cloud-native applications and services. I recently wrote and published an article related to this on DZone.
Performance Improvements
.NET 9 is focused on optimizing cloud-native apps, and performance is a key aspect of this optimization. Several performance-related improvements have been made in .NET 9, including:
1. Faster Exceptions
Exceptions are now 2-4x faster in .NET 9, thanks to a more modern implementation. This improvement means that your app will spend less time handling exceptions, allowing it to focus on its core functionality.
2. Faster Loops
Loop performance has been improved in .NET 9 through loop hoisting and induction variable widening. These optimizations allow loops to run faster and more efficiently, making your app more responsive.
3. Dynamic PGO Improvements
Dynamic PGO (Profile-Guided Optimization) has been improved in .NET 9, reducing the cost of type checks. This means that your app will run faster and more efficiently, with less overhead from type checks.
4. RyuJIT Improvements
RyuJIT, the .NET Just-In-Time compiler, has been improved in .NET 9 to inline more generic methods. This means that your app will run faster, with less overhead from method calls.
5. Arm64 Code Optimizations
Arm64 code can now be written to be much faster using SVE/SVE2 SIMD instructions on Arm64. This means that your app can take advantage of the latest Arm64 hardware, running faster and more efficiently.
6. Server GC Mode
The new server GC mode in .NET 9 has been shown to reduce memory usage by up to 2/3 in some benchmarks. This means that your app will use less memory, reducing costs and improving performance.
These performance-related improvements in .NET 9 mean that your app will run faster, leaner, and more efficiently. Whether you're building a cloud-native app or a desktop app, .NET 9 has the performance optimizations you need to succeed.
AI-Related Improvements
These AI-related improvements in .NET enable developers to build powerful applications with AI capabilities, integrate with the AI ecosystem, and monitor and observe AI app performance. Multiple partnerships include Qdrant, Milvus, Weaviate, and more to expand the .NET AI ecosystem. It is easy to integrate with Semantic Kernel, Azure SQL, and Azure AI search.
Feature | Improvement | Benefit |
---|---|---|
Tensor<T> | New type for tensors | Effective data handling and information flow for learning and prediction |
Smart Components | Prebuilt controls with end-to-end AI features | Infuse apps with AI capabilities in minutes |
OpenAI SDK | Official .NET library for OpenAI | Delightful experience and parity with other programming languages |
Monitoring and Observing | Features for monitoring and tracing AI apps | Reliable, performant, and high-quality outcomes |
Note: There is some integration work within the .NET Aspire team, semantic kernel, and Azure to utilize the .NET Aspire dashboard to collect and track metrics.
Web-Related Improvements
- Improved performance, security, and reliability
- Upgrades to existing ASP.NET Core features for modern cloud-native apps
- Built-in support for OpenAPI document generation
- Ability to generate OpenAPI documents at build-time or runtime
- Customizable OpenAPI documents using document and operation transformers
These improvements aim to enhance the web development experience with .NET and ASP.NET Core, making it easier to build modern web apps with improved quality and fundamentals.
Caching Improvements With HybridCache
As one of my favorites, I will explain more in-depth along with code samples in a different article about HybridCache
.
In short, The HybridCache
API in ASP.NET Core is upgraded to provide a more efficient and scalable caching solution. It introduces a multi-tier storage approach, combining in-process (L1) and out-of-process (L2) caches, with features like "stampede" protection and configurable serialization. This results in significantly faster performance, with up to 1000x improvement in high cache hit rate scenarios.
C# 13: Introducing New Language Features
C# 13 brings forth a range of language elements aimed at enhancing code clarity, maintainability, and developer efficiency. Here are some key additions:
params
collections: Theparams
keyword is no longer restricted to just array types. It can now be used with any collection type that is recognized, includingSystem.Span<T>
,System.ReadOnlySpan<T>
, and types that implementSystem.Collections.Generic.IEnumerable<T>
and have anAdd
method. This provides greater flexibility when working with methods that need to accept a variable number of arguments.- In the code snippet below, the
PrintNumbers
method accepts aparams
of typeList<int>[]
, which means you can pass any number ofList<int>
arguments to the method.
- In the code snippet below, the
public void PrintNumbers(params List<int>[] numbersLists)
{
foreach (var numbers in numbersLists)
{
foreach (var number in numbers)
{
Console.WriteLine(number);
}
}
}
PrintNumbers(new List<int> {1, 2, 3}, new List<int> {4, 5, 6}, new List<int> {7, 8, 9});
- New lock object:
System.Threading.Lock
has been introduced to provide better thread synchronization through its API. - New escape sequence: You can use
\e
as a character literal escape sequence for theESCAPE
character, Unicode U+001B. - Method group natural type improvements: This feature makes small optimizations to overload resolution involving method groups.
- Implicit indexer access in object initializers: The
^
operator allows us to use an indexer directly within an object initializer.
Conclusion
C# 13 and .NET 9 mark a crucial step towards the advancement of C# programming and the .NET environment. The latest release brings a host of new features and improvements that enhance developer productivity, application performance, and security. By staying up-to-date with these changes, developers can leverage these advancements to build more robust, efficient, and secure applications. Happy coding!
Opinions expressed by DZone contributors are their own.
Comments