Advertisement
Beginner C#, OOP

How do properties provide encapsulation?

Short answer

Properties expose get/set accessors over a private field, letting you validate on set and compute on get while keeping the field hidden.

Save C# mock interview
Advertisement
AModel answer
Properties expose get/set accessors over a private field, letting you validate on set and compute on get while keeping the field hidden.
public int Age { get; private set; }
public decimal Discount { get => _d; set => _d = Math.Clamp(value,0,100); }
Real-world example
Clamping a discount to 0-100 so no caller can set an invalid value.