All rules
IDE0064Language rules (modifier preferences)
Make struct fields writable
Make struct fields writable
Microsoft docsDescription
This rule detects structs that contain one or more readonly fields and also contain an assignment to this outside of the constructor. The rule recommends converting readonly fields to non-read only, that is, writable. Marking such struct fields as readonly can lead to unexpected behavior, because the value assigned to the field can change when this is assigned outside the constructor.
Example
// Code with violations
struct MyStruct
{
public readonly int Value;
public MyStruct(int value)
{
Value = value;
}
public void Test()
{
this = new MyStruct(5);
}
}
// Fixed code
struct MyStruct
{
public int Value;
public MyStruct(int value)
{
Value = value;
}
public void Test()
{
this = new MyStruct(5);
}
}Your vote
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0