.NET 7 Guide – What’s New in .NET?
17/01/2023

.NET 7 Guide – What’s New in .NET?

Table of contents:

    .NET is a powerful developer platform providing developers with programming languages, libraries, and tools for creating many different types of web and mobile apps. 

    A new version of .NET gets released every November, so it’s high time we got our hands on .NET 7 which has been in the making for some time.

    This article will:

    • present the state of .NET technology,
    • discuss the top benefits it delivers,
    • go through the freshest features of the 7th version of the dot net platform,
    • briefly mention why we like working with .NET at CSHARK.

    Let’s see what Microsoft has in store for us this year.

    What is .NET?

    As a quick reminder – .NET was designed by Microsoft as a free, open-source developer platform consisting of tools, programming languages, and libraries for building applications of various kinds. 

    You can write .NET apps using well-known languages:

    • C# – a modern, simple, object-oriented, type-safe programming language,
    • F# – a functional-oriented programming language with lightweight syntax perfect for writing succinct, performant, and robust code,
    • Visual Basic – an event-driven programming language by Microsoft.

    By using .NET, developers get access to multiple languages, editors, and libraries that can be used for:

    • building web apps and services for Windows, macOS, Linux, Docker,
    • developing native mobile apps for iOS and Android,
    • creating native apps for macOS and Windows,
    • consuming cloud services or creating new ones,
    • machine learning by using algorithms, predictive models, and speech processing in the apps,
    • IoT apps with native support for Raspberry Pi,
    • independently deployable microservices that can run on Docker containers,
    • 2D and 3D games for consoles, desktops, and mobile phones.

    Top Benefits of .NET From Project and Business Perspectives

    .NET was designed to reduce programming errors by following a modular approach to software design. What other benefits does .NET bring to the table?

    • Productivity

    .NET allows faster development of high-quality apps, providing developers with constructs like asynchronous programming, generics, and Language Integrated Query (LINQ). Multi-language support, extensive class libraries, powerful tooling delivered by the Visual Studio family, as well as common APIs are what make .NET the most productive platform.

    • Wide Compatibility

    With .NET, you can build apps for any platform. This means developers can reuse one code while building mobile applications, server applications, or high-scale cloud microservices.

    • Community Love and Appreciation

    .NET was ranked one of the top most-loved frameworks on the Stack Overflow Developer Survey for four consecutive years (from 2019 to 2022).

    • Amazing Performance

    .NET is super fast, which means your apps have better response times and need less computing power. When benchmarked against tasks like JSON serialization, server-side template rendering, or database access .NET gets the job done quicker than any other framework.

    • Top Security and Stability

    .NET is supported by Microsoft, which takes security very seriously. The platform is frequently updated to address any potential security threats quickly and effectively.

    • Large Ecosystem

    There are over 5,000,000 .NET developers all around the world. This means you can leverage the ecosystem and easily find answers to technical challenges. From the business perspective, this means the high availability of developers when scaling the project.

    • A Popular Choice of The Big Guys

    Worldwide customers rely on .NET to help them address their business needs and solve industry problems. Among them are Stack Overflow, UPS, GE Aviation, GoDaddy, Microsoft Teams, and the American Cancer Society. .NET is prepared to operate in an enterprise environment and gives the ability to scale the business with no downtime.

    • One API

    .NET gives a standard set of class libraries and APIs that are used by all .NET apps. Each app model can also expose additional APIs specific to a given operating system.

    • Active Community

    .NET is open source and fosters open development and collaboration around the ecosystem. This means thousands of developers contribute to the platform.

    • Visual Studio Product Family

    Tools delivered by .NET provide a great development experience with thousands of editors and extensions.

    As we already mentioned, .NET is supported and developed by Microsoft, and they take care of its regular updates.

    So, What is .NET 7?

    .NET 7 is the newest version of the platform which delivers important changes while still continuing the legacy of .NET 5. This means we can see a focus on the integration and unification of the platform’s modules.

    .NET 7 was released to further optimize the platform, increasing its effectiveness and speed. Those speed changes are a result of introducing the On-Stack Replacement mechanism (OSR) in 2020.

    .NET 7 Release Date

    A new version of the platform gets released every year. .NET 7 was officially released on November 8th, 2022, replacing .NET 6.

    .NET 7 Features

    Managed Extensibility Framework Update

    Starting from this update, the new APIs allow you to add a single object instance to the `System.Composition.Hosting`.

    Observability

    Bringing improvements in support for the cloud-native specification (OpenTelemetry), however, it’s still under development.

    Robust X.500 Names Generation

    Microsoft has also focused on cryptographic security by making building on `X500DistinguishedName` easier and more secure.

    Blazor and .NET MAUI Union

    Blazor is a web app development framework and one of its main features is the ability to run the application views on the client side. On the other hand, .NET MAUI is Microsoft’s framework focusing on mobile applications and is said to be the evolution of Xamarin. That being said, Microsoft is currently working on blending those two together and creating Blazor Hybrid for building native apps.

    Modern Cloud

    .NET 7 brings facilitation of creating cloud-native applications with various improvements concerning the developer experience. This will simplify the installation and configuration of the authentication and authorization system, and bring small performance improvements at application runtime.

    Better Hot Reload

    C# Hot Reload is now available in Blazor WebAssembly and .NET for Android and iOS. It also includes features like adding static lambdas to existing methods, adding new classes, adding new static or non-virtual instance methods to existing classes, adding lambdas that capture this to existing methods that already have at least one lambda that captures this, and adding new static fields to existing classes.

    Easier Upgrades of Applications

    Migrating applications to .NET 6 wasn’t the easiest, but the new version brings upgrade enhancements and .NET Upgrade Assistant that will greatly help in this endeavor. 

    Serialization and deserialization improvements

    `System.Text.Json` now includes improvements like `JsonSerializerOptions`, `JsonWriter Options.MaxDepth` property and `Patch` methods in `System.Net.Http.Json`. Thanks to these additions, serialization, and deserialization of polymorphic type hierarchies are now possible.

    CopyString UTF-8 & UTF-16

    Now with `CopyString` you can copy UTF-8 or even UTF-16 strings without decoding thanks to new methods that are able to consume decoded JSON strings.

    Activity.Current New Standard

    In .NET 6, `AsyncLocal<T>` is used to achieve span context tracking of different threads. In .NET 7 you can use `Activity.CurrentChange` to receive notifications for value changes.

    Exposed Methods

    This feature resolves the problem of performance tests showing many allocations incurred when using enumeration interfaces. By using it, you can enumerate properties with quick access to the elements without extra allocations.

    Microsecond and Nanosecond Values

    With .NET 7 you can now add microsecond and nanosecond values to the different time and date structures. Previously you had to determine their value based on the time increment called ‘tick’ (equals 100ns).

    Single Memory Cache

    .NET 7 allows you to instantiate a single memory cache using the `AddMemoryCache` API. You can also get it injected in order to call `GetCurrentStatistics`.

    Multiple Memory Cache

    Just like with the single memory cache, you can also instantiate multiple memory caches with `GetCurrentStatistics`.

    New Command Names

    Command lines are changing – commands in this output no longer need the - - prefix, but don’t worry, the old versions of the commands are still available.

    Tab Key Completion

    The `dotnet` command line interface already supports tab completion on shells like PowerShell, bash, zsh, and fish. .NET 7 brought the `new` command a lot of functionalities:

    Dynamic PGO Improvements

    Dynamic PGO was created by Microsoft to introduce changes from the Static PGO. What is different here is that Dynamic PGO doesn’t require any special tools for optimization.

    New Tar APIs

    With the 2022 .NET update, we got cross-platform APIs that can modify and extract `tar` archives.

    OSR (On Stack Replacement)

    On Stack Replacement works great with tiered compilation. This gives you the ability to change the code that is being executed while executing a method. OSR gives you an extra 25% of speed at startup, and the overall improvements can range from 10 to 30%.

    .NET 7 Performance

    It’s the main area that has been improved in .NET 7. Microsoft provides a 232 pages PDF document describing over 500 improvements. You can find all of them in the Microsoft Developers blog post. We’re going to focus only on a few.

    Reflection

    It has always been a powerful tool to query all the metadata and call arbitrary functionality dynamically. Using reflection, you’re even able to emit dynamically generated methods at runtime.

    One of the most impacted areas is reflection invoke. One of its use cases is creating a delegate from the `MethodBase` via `CreateDelegate<T>` and using that delegate for all future invocations. It’s a way to optimize invocation speed but only assuming you know the signature of the method in advance. In some cases, you don’t know the signature at compile time. To address this, reflection is being used to generate code at run-time. This is extremely expensive and complex to do. .NET 7 comes with improvements in that area. `Invoke` will use reflection emit by itself to generate a customized delegate for invoking the target method – and that has a lot of performance benefits.

    Source: https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#reflection 

    Regex

    Parsing regular expressions has got significant optimization in .NET 7. Improved strategies for string processing have paid off. Let’s see a couple of examples.

    Following pattern `(@"[\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?"` says about finding fragments which begin with a word character (`\w`), which includes ~50,000 of the ~65,000 possible characters.

    Source: https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#regex

    For a pattern with anchors, like `^abc|^def`, the existing analysis had trouble seeing through that alternation to find the guaranteed starting `^` anchor. In .NET 7 it has been improved significantly.

    Source: https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#regex

    Another example is also about character class matching but it can be found as a more real-world use case, like recognizing sets like `[\d\D]`, `[\s\S]` and `[\w\W]`, as meaning “match anything”.

    Source: https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#regex 

    Regex Source Generator Improvements

    Thanks to the new Regex Source Generator, you can limit the time spent on optimizing patterns up to 5x times. What’s more, it operates within a `partial` class so there’s no overhead of compiling time for instances.

    Analyzer

    We got a new analyzer that looks for the `Regex` uses with the possibility of conversion for the use of the `RegexGenerator` source generator. Microsoft says that the generator output depends on the known values of the parameters and requires LangVersion for preview.

    Code Fixer

    Code fixer can now suggest `RegexGenerator` source generator methods and has the ability to override the names that come by default. Another function of this code fixer is to replace the original code through a call to the new method.

    Improved Startup Time

    With Microsoft focusing mainly on performance improvements, it’s not a surprise that with .NET came reduced startup time (according to Microsoft, up to 10-15%). This can be achieved due to reducing the number of modifications after code creation at runtime.

    Native AOT

    Native AOT (Ahead-of-time) is one of the new improvements to .NET. AOT generates code at compile-time instead of run-time. This brings performance improvements in startup time, memory usage, disk size, and access to restricted platforms. It’s worth mentioning that .NET also offers `ReadyToRun` and `Nano AOT`, and AOT didn’t replace any of them. 

    Entity Framework Core 7

    One of the new features in EF Core 7 is that updating and deleting entities has been moved to the database side. That means it’s not being loaded to the memory at first before the action anymore. It has affected the performance a lot.

    Source: https://youtu.be/1U02rnSaz9Q 

    gRPC

    For gRPC to support multiplexing, HTTP/2 is required. However, a known issue in Kestrel’s implementation of HTTP/2 caused a bottleneck during the writing of responses over HTTP/2 when the connection was busy. The connection was made with thread locks in .NET 6, which caused lock contention.

    In .NET 7 the bottleneck has been resolved by implementing a queue that notifies all other threads when a write has been completed. Therefore, the performance has been improved because there’s no need to fight over locks anymore.

    Source: https://youtu.be/et_2NBk4N4Y

    Critical Performance Tags

    Now it’s possible to expose tag enumerator methods. This is of great use in scenarios where performance matters the most. In .NET 7 object enumerations are performed with no loss of performance or speed.

    Loop Optimizations

    Even though it might seem like a minor improvement, it still delivers performance enhancements. Microsoft eliminated the initialization conditions of the loop cloning variables, which allows a 21% performance increase.

    How to Migrate to .NET 7?

    If you want to experience .NET 7 for yourself, you might run into some issues while upgrading your current app. How to do it properly? Fortunately, there’s a highly customizable reverse proxy built on the .NET platform – YARP. It’s available as a NuGet package. YARP combined with Strangler Fig Pattern can be one of the simplest ways to migrate your app. You can find more info on that in this video.

    To learn more about YARP, visit its website or GitHub repository.

    .NET and CSHARK – Why Do We Work With .NET?

    As a development company, we have deep expertise in .NET as most of our products were built using the platform. We use .NET to develop small and large applications, as well as to scale the existing solutions.

    Our development teams stay on track with the latest approaches to .NET programming, using SaaS and cloud services, and implementing AWS and Azure solutions.

    We adore .NET for its maturity, security, and availability of various tools. Behind the platform stands a huge community, which is always there to provide support and help solve any development mysteries.

    If you’re looking for a technology partner fluent in .NET technology, see how we work, and let us help you smoothly navigate your challenge today. Unlock the possibility for .NET technology solutions with our development experience.

     
    CONSULT YOUR .NET PROJECT
    LET’S TALK
     
    Paweł Świerblewski
    Senior .NET Developer at CSHARK. Has a strong emphasis on clean architecture and high-quality code.