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.