Model valid states with modern C#
8 steps · about 3 h 35 min · Intermediate
- Turn missing-value assumptions into compiler checks (guide, Microsoft Learn, 35 min)
Nullable annotations expose unchecked absence at the point it enters the model.
- Represent immutable values with records (guide, Microsoft Learn, 30 min)
Records provide concise construction, copying and value equality for domain data.
- Choose record class or record struct (guide, Microsoft Learn, 25 min)
Reference and value semantics affect identity, copying and default behaviour.
- Branch on shapes with pattern matching (guide, Microsoft Learn, 35 min)
Property and relational patterns express state rules without fragile casts.
- Make state mappings exhaustive (guide, Microsoft Learn, 25 min)
A switch expression keeps input shapes and their outputs in one checked expression.
- Allow construction without later mutation (guide, Microsoft Learn, 20 min)
Init-only setters support object initialisers while closing the value after creation.
- Require essential members at creation (guide, Microsoft Learn, 20 min)
Required members make omitted construction data a compile-time warning instead of a distant failure.
- Reject invalid construction clearly (guide, Microsoft Learn, 25 min)
Use focused exceptions for violated invariants while keeping routine alternatives in the type model.