C# Concept - Properties with validation
Overview
Section titled βOverviewβValidated properties are a property design pattern, not a standalone C# release feature. They rely on ordinary property accessors and guard clauses.
Introduced In
Section titled βIntroduced InβThis pattern has been possible since C# 1.0 (2002).
Example
Section titled βExampleβpublic class Person{ private int _age;
public int Age { get => _age; set { if (value < 0 || value > 120) throw new ArgumentOutOfRangeException(nameof(value)); _age = value; } }}- Constructor validation is often a better choice for immutable models.
- Keep setter validation focused and predictable.