Model valid states with modern C#

8 steps · about 3 h 35 min · Intermediate

  1. Turn missing-value assumptions into compiler checks (guide, Microsoft Learn, 35 min)

    Nullable annotations expose unchecked absence at the point it enters the model.

  2. Represent immutable values with records (guide, Microsoft Learn, 30 min)

    Records provide concise construction, copying and value equality for domain data.

  3. Choose record class or record struct (guide, Microsoft Learn, 25 min)

    Reference and value semantics affect identity, copying and default behaviour.

  4. Branch on shapes with pattern matching (guide, Microsoft Learn, 35 min)

    Property and relational patterns express state rules without fragile casts.

  5. Make state mappings exhaustive (guide, Microsoft Learn, 25 min)

    A switch expression keeps input shapes and their outputs in one checked expression.

  6. Allow construction without later mutation (guide, Microsoft Learn, 20 min)

    Init-only setters support object initialisers while closing the value after creation.

  7. 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.

  8. Reject invalid construction clearly (guide, Microsoft Learn, 25 min)

    Use focused exceptions for violated invariants while keeping routine alternatives in the type model.