A new lock type in .NET 9

12/27/2023
3 minute read

There is a new sheriff in town when it comes to the lock keyword, And that is the new System.Threading.Lock type that is introduced in .NET 9. And yes, I know - we still need time to digest the big .NET 8 release.

What is the System.Threading.Lock type?

Small disclaimer at the beginning: This feature/type is still in preview mode. You can download daily builds of .NET 9 and opt-in to the new Lock type via the <EnablePreviewFeatures>true</EnablePreviewFeatures> toggle in your csproj file. That said - there might be breaking changes in the future.

The new Lock type (sitting in System.Threading.Lock) has one main purpose: Being a lock. Yes, you can thank me later for that information. But on a serious note, the new type is a lock and a lock only. There is no ambiguity about what it does. Until now, we have done something like:

private readonly object _lock = new object();

public void DoSomething()
{
    lock (_lock)
    {
        // Do something
    }
}

The new Lock type is a bit more explicit about what it does:

private readonly Lock _lock = new Lock();

public void DoSomething()
{
    lock (_lock)
    {
        // Do something
    }
}

The usage is absolutely the same (currently, more to that later). But the new type is very explicit and, in theory, can be faster. It depends on the underlying implementation that is used. Until now, always a Monitor was involved, but that doesn't have to be from now on (well from November 2024).

But the story doesn't end here necessarily. There is a proposal: "Lock statement pattern ". The idea would be to have a bunch of whole new Lock types. Well, a marker interface like ILockPattern that is "special handled" by the compiler. So given code like this:

class MyDataStructure
{
    private readonly Lock _lock = new();

    void Foo()
    {
        lock (_lock)
        {
            // do something
        }
    }
}

Will be lowered by the compiler as such:

class MyDataStructure
{
    private readonly Lock _lock = new();

    void Foo()
    {
        using (_lock.EnterLockScope())
        {
            // do something
        }
    }
}

Personally, I like reusing the using pattern for such occasions rather than having "multiple" meanings of lock (one for the Monitor and one for the Lock type or whatever underlying type is used). Like Stephen Clearly's AsyncEx library does it:

AsyncLock _mutex = new AsyncLock();
using (await _mutex.LockAsync())
{
    // do something
}

.NET developers are used to the using pattern, and it is a very clear and concise way to express the intention of the code. As this is just a proposal, we will see where we will land.

Three new LINQ methods in .NET 9

Even though we are in the alpha of .NET 9 and .NET 8 was released not more than two months ago, the dotnet team does not sleep and pushes new changes! In this blog post, we are checking what new methods were added to everyones favorite: LINQ.

Lock statement patterns

A few weeks back, I wrote an article "A new lock type in .NET 9" where I showcased the new Lock type. Nothing fancy - well, at least it was more expressive. But now the dotnet team went a step further with this!

ReadOnlySet<T> in .NET 9

The next preview (preview 6) will bring a new type ReadOnlySet<T>. This is a read-only set that is similar to ReadOnlyCollection<T>. Let's see how it works and why it was introduced.

An error has occurred. This application may no longer respond until reloaded. Reload x