Property Accessor¶
What does this mean ?¶
A property accessor implements the behavior of reading or assigning a property. A setter that ignores its incoming value, writes the wrong field or calls itself recursively can violate the property's contract. Some properties are intentionally computed or read-only, so a backing field is not always required.
What can happen ?¶
Callers may observe stale or incorrect state, or recursion may exhaust the stack. These are correctness issues; a security consequence requires a concrete affected control or invariant.
Recommendation¶
Use an auto-property for simple storage, or a backing field when validation is needed. Validate before changing state. If a property cannot be assigned meaningfully, prefer removing/restricting its setter rather than silently ignoring the assignment.
Sample Code¶
// Incorrect for an ordinary count property: every assignment becomes 42.
private int brokenCount;
public int BrokenCount { get => brokenCount; set => brokenCount = 42; }
private int count;
public int Count
{
get => count;
set
{
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value));
count = value;
}
}
These members belong to a class. The nonnegative constraint is this example's domain rule, not a universal requirement for integer properties.
Regression checks¶
Set two distinct valid values and confirm the getter reflects each one. Verify invalid input leaves the previous state intact. Check unrelated properties are unchanged and watch for accidental self-recursion.