.NET 9 — ToList vs ToArray

Performance comparison between ToList and ToArray

Last year I made an article comparing the performance of ToList versus ToArray when creating short lived collections that won’t be mutated, usually used to prevent multiple enumerations when iterating over a temporary LINQ transformation or to ensure mapping exceptions will be thrown inside the corresponding application layer.

The tests were performed with .NET Framework 4.8, .NET 7 and .NET 8, which concluded that ToArray is significantly faster and more memory efficient for almost all collection sizes, with the only exception with very large collections in .NET 8 were ToList was faster - but still uses more memory).

Assuming everything goes as planed, Microsoft should release .NET 9 by the end of 2024. This is the next major version of their most popular development framework that will bring a lot of new features (C# 13 is one of them) and performance improvements.

Read More

.NET — IAsyncEnumerable utility extensions

Collection of utility extensions for async streams

With the introduction of async streams in .NET Core 3, represented by the interface IAsyncEnumerable<T>, and with a direct support in C# 8 to iterate using await foreach or easily implement a new asynchronous stream by defining async IAsyncEnumerable<T> as the method result and using yield return/yield break just like we did for IEnumerable<T>, Microsoft standardized the way .NET developers implement asynchronous streams.

Even if we don’t realize, we probably use async streams on a daily basis, from Entity Framework Core to ASP.NET Core, it has become an important part of .NET that is now widely adopted.
In this article I’m going to show some the most common scenarios I usually face when working directly with IAsyncEnumerable<T> and how I usually solve them.


Read More

.NET 9 — Exception handling performance

Comparing exception handling performance over time

Assuming everything goes as planed, by the end of 2024 Microsoft should release .NET 9, which is the next major version of their most popular development framework.

It will bring a lot of new features (C# 13 is one of them) but also a lot of performance improvements, which have been a major focus ever since Microsoft created the first version of .NET Core.

Even if an application doesn’t rely on exceptions to hard stop process flows, usually very important for high demanding applications, it certainly connects to a lot of external systems, like databases, message buses, caches and even HTTP endpoints, which may cause exceptions at any moment.

Read More

.NET — Hangfire

Using Hangfire to recurrently trigger HTTP endpoints

One common scenario when developing applications is the need to perform background processing than can either be run only once (like sending an email) or scheduled to be run multiple times within a given interval (like doing database housekeeping), usually defined by a cron expression.

When I need to implement such requirements my first choice for the last few years as always been Hangfire.

It integrates seamlessly with ASP.NET Core applications, it has a simple but very powerful dashboard to monitor and manually trigger recurring jobs and is open source and completely free for commercial use.

Read More

.NET — LinkedList vs ToArray

Performance comparison between LinkedList and ToArray

Some weeks ago I created an article comparing the performance of ToList versus ToArray when creating short lived collections that won’t be mutated, usually used to prevent multiple enumerations when iterating over a temporary LINQ transformation or to ensure mapping exceptions will be thrown inside the corresponding application layer.

In that article I concluded ToArray is faster and more memory efficient than ToList for almost any collection sizes and in any .NET version — tests were conducted on .NET Framework 4.8, .NET 7 and .NET 8.

But then I began to wonder: If I’m creating a temporary collection of unknown size just to force enumeration, wouldn’t LinkedList be a more efficient collection since I’m only appending items to the end, which is O(1), instead of constantly allocating new arrays like ToArray does?

Read More