All rules
IDE0044Modifier preferences MS default: Suggestion

Add readonly modifier

Mark fields that are only assigned in the constructor as `readonly`.

Microsoft docs

Description

Flags private fields that are never reassigned after construction so they can be marked readonly.

Why it matters

Readonly fields communicate immutability and let the compiler enforce that the field is not reassigned.

Examples

Avoid
private ILogger _logger;
public Service(ILogger logger) => _logger = logger;
Prefer
private readonly ILogger _logger;
public Service(ILogger logger) => _logger = logger;

Configurable options

Vote for the value each option should take in the generated .editorconfig.

dotnet_style_readonly_field
default: true
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0