Properties
Context: Properties provide controlled access to private fields using get and set accessors. They encapsulate validation and logic.
Basic Property
Section titled “Basic Property”private int _age;public int Age{ get { return _age; } set { _age = value; }}Expression‑Bodied Syntax (C# 7+)
Section titled “Expression‑Bodied Syntax (C# 7+)”public int Age{ get => _age; set => _age = value;}Read‑Only Property (only getter)
Section titled “Read‑Only Property (only getter)”public string FullName => $"{FirstName} {LastName}";Write‑Only Property (only setter)
Section titled “Write‑Only Property (only setter)”private string _secret;public string Secret { set => _secret = value; }Validation in Setter
Section titled “Validation in Setter”private int _score;public int Score{ get => _score; set { if (value >= 0 && value <= 100) _score = value; else throw new ArgumentOutOfRangeException(); }}