Skip to content

C# Concept - Properties with validation

Validated properties are a property design pattern, not a standalone C# release feature. They rely on ordinary property accessors and guard clauses.

This pattern has been possible since C# 1.0 (2002).

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.