AL0011: Avoid lock keyword on non-Lock types
The lock keyword on object/non-Lock types should be replaced with the Lock class from System.Threading.
When it triggers
private readonly object _syncRoot = new();
// AL0011: Use 'Lock _lock = new()' with 'lock(_lock)' instead
lock (_syncRoot)
{
// Critical section
}
Why this matters
.NET 9 introduced System.Threading.Lock, a dedicated type for synchronization that provides better performance than locking on object. Using Lock also makes the intent clearer and enables future runtime optimizations.
How to fix
Replace object with Lock:
private readonly Lock _lock = new();
lock (_lock)
{
// Critical section
}
Requirements
- .NET 9.0 or later
- C# 13 or later
Configuration
[*.cs]
dotnet_diagnostic.AL0011.severity = warning