Skip to main content
Source: Al0057ToAl0060ThreadingAnalyzer.cs

Description

Locking on this exposes the synchronization target to any code that holds a reference to the instance, creating deadlock risks.

Bad Code

public void Update()
{
    lock (this) { _count++; }
}

Good Code

private readonly Lock _syncRoot = new();
public void Update()
{
    lock (_syncRoot) { _count++; }
}

Properties

  • Category: Threading
  • Severity: Warning
  • Enabled by default: True
  • Code fix available: False

Configuration

dotnet_diagnostic.AL0058.severity = warning