
The .NET 7 framework has just released some amazing new features that will change the way you code. They haven’t been officially released yet, but you can start playing with them and get ready for when it officially launched in november of this year!
The newest preview of the .NET Framework has been released to the public and it’s full of surprises. Check out these Top 7 features in the .NET 7 release that will change the way you code forever!
Activity.Current New Standard
Currently in .NET 6, to achieve span context tracking of the different threads being managed, the most common is to use AsyncLocal<T>
for this purpose.
According to Jeremy Likness in his Announcing .NET 7 Preview 4 post:
“…with
Activity
becoming the standard to represent spans, as used by OpenTelemetry, it is impossible to set the value changed handler since the context is trackedvia Activity.Current
.”
Now with Activity.CurrentChanged
we will be able to achieve this to receive notifications. Let’s see Microsoft example:
And this is how it would be used:
📚 For more in-depth details, I recomend you to check the original proposal: Api handle Activity.Current value changes
Exposed Methods in performance-critical scenarios
The main problem this new feature solves, as Mikel Blanchard relates, is that performance tests show many allocations incurred when using enumeration interfaces.
This can now be solved by using exposed methods to enumerate properties with quick access to the elements and with no extra allocations.
Let’s see Microsoft example:
And this is how it would be used:
📚 For more in-depth details, I recomend you to check the original proposal: System.Diagnostics.Activity: Enumeration API
Microseconds and Nanoseconds in date/time structures
The smallest time increment that could be used was the “tick” and its value is 100ns. The problem with this is that to determine a value in microseconds or nanoseconds you had to calculate everything based on the “tick” and this was not the most optimal thing in the world.
As Microsoft reports, they will now add microsecond and nanosecond values to the different date and time structures that exist.
Let’s see Microsoft example:
With DateTime
With TimeOnly:
📚 For more in-depth details, I recomend you to check the original proposal: Add Microseconds and Nanoseconds to TimeStamp, DateTime, DateTimeOffset, and TimeOnly
Single Memory Cache
Now you can instantiate a single memory cache with the AddMemoryCache
API. In addition, you will be able to get it injected so you can call GetCurrentStatistics
. Let’s check Microsoft example:
In addition, Microsoft leaves an example of how it would help us to see stats with the dotnet-counters
tool (check it here):
📚 For more in-depth details, I recomend you to check the original proposal: Let consumers of MemoryCache access metrics
Multiple Memory Cache
As in the previous feature, which allowed instantiating a single cache memory, we can also instantiate multiple memory cache with GetCurrentStatistics
. Let’s check this Microsoft example:
And also, as in the previous feature, Microsoft shows us that we can also measure stats with the dotnet-counters
tool (check it here):
📚 For more in-depth details, I recomend you to check the original proposal: Let consumers of MemoryCache access metrics
New Tar APIs
We will now have cross-platform APIS with which we can extract and modify (read and write) tar archives. As usual, Microsoft has shown examples so let’s take a look at some of them:
Archive
Extract
📚 For more in-depth details, I recomend you to check the original proposal: Implement Tar APIs
OSR (On Stack Replacement)
OSR (On Stack Replacement) is a great complement to tiered compilation. It allows, in the middle of the execution of a method, to change the code that is being executed by the methods that are being executed at the moment.
According to Microsoft:
“OSR allows long-running methods to switch to more optimized versions mid-execution, so the runtime can jit all methods quickly at first and then transition to more optimized versions when those methods are called frequently (via tiered compilation) or have long-running loops (via OSR).”
With OSR, we can obtain up to 25% extra speed at start-up (Avalonia IL Spy test) and according to TechEmpower, improvements can range from 10% to 30%.

📚 If you want to know in how OSR works, please refer: OSR Document