Unassigned Readonly Field
ID |
csharp.unassigned_readonly_field |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
field, reliability |
Description
Reports readonly fields that have neither an inline initializer nor an assignment in
any constructor of the enclosing type. Such fields are silently left at the type’s
default value (zero, false, or null).
Rationale
readonly advertises that the value is set once and never mutated again. If no
initialisation runs, every consumer reads the default value, which usually leads to
`NullReferenceException`s or subtle wrong-default bugs. The compiler will not complain
because a missing assignment is a valid program, but it is almost never the developer’s
intent.
public class Account
{
private readonly string _id; // FLAW — never assigned
private readonly DateTime _created; // FLAW
public Account()
{
// forgot to assign _id and _created
}
}
public class AccountOk
{
private readonly string _id = Guid.NewGuid().ToString(); // OK
private readonly DateTime _created;
public AccountOk(DateTime created)
{
_created = created; // OK
}
}
Remediation
Assign the field either inline at the declaration or unconditionally in every
constructor. If the value cannot be known until after construction, the field probably
should not be readonly.