.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

.NET — TaskCompletionSource and CancellationTokenSource

The importance of TCS and CTS for Task-based asynchronous programming

When Microsoft released .NET Framework 4.0 in April 2010, the Task Parallel Library (TPL) was introduced to help developers replace the previously used Asynchronous Programming Model (APM) pattern for a Task-based asynchronous programming.

Before the introduction of Tasks, when implementing asynchronous code, developers had to define two variations of the same method: one to begin the operation execution (convention: BeginOperationName), that would receive an optional callback to be invoked when completed, and another to wait for the operation to complete (convention: EndOperationName) and get the result or an exception, usually used inside the callback to prevent the main thread to be blocked.


Read More

Dispose pattern in .NET

Implementing IDisposable and IAsyncDisposable interfaces

When working with .NET, you’ll often hear about the garbage collector and how it manages the allocation and release the application’s memory from the managed heap, making our life easier.

This is true for the majority of objects but sometimes we have to work with unmanaged resources — files, network or database connections — that we must explicitly release since the garbage collector doesn’t know how to do the cleanup for us, despite being able to track the object that encapsulates the unmanaged resource.

To help preventing memory leaks, .NET provides a simple and standard way to cleanup unmanaged resources called dispose pattern, which consists in implementing the IDisposable interface and calling Dispose method when resources aren’t needed, which is usually done via the using keyword.

Read More