AL0001: Prohibit reassignment of primary constructor parameters
Primary constructor parameters should not be reassigned as this can lead to confusion and bugs.
When it triggers
// Error: Primary constructor parameter 'x' should not be reassigned
public class Example(int x)
{
public void SetX(int value) => x = value; // AL0001
public void Increment() => x++; // AL0001
}
Why this matters
Primary constructor parameters are captured by the compiler and may be used for multiple purposes:
- Field initialization
- Property backing fields
- Constructor logic
Reassigning them creates confusion about which value is actually used and can lead to subtle bugs.
How to fix
Use a separate field if you need mutable state:
public class Example(int x)
{
private int _x = x;
public void SetX(int value) => _x = value;
public void Increment() => _x++;
}
Configuration
[*.cs]
dotnet_diagnostic.AL0001.severity = error