Table of Contents

Expression Bodies

When to use => vs { return ...; }.

Rules

Member Type Body Style Enforced
Methods Block body { } Error
Constructors Block body { } Error
Operators Block body { } Error
Properties (get-only) Expression body => Error
Indexers Expression body => Error
Accessors (get/set one-liners) Expression body => Error

Properties & Indexers — Use Expression Body

// GOOD
public int Health => _health;
public string Name => _name;
public Item this[int index] => _items[index];

// BAD
public int Health
{
    get { return _health; }
}

Methods — Use Block Body

// GOOD
public int GetHealth()
{
    return _health;
}

// BAD
public int GetHealth() => _health;

Why Block Body for Methods?

  • Debugger breakpoints work on individual lines inside blocks
  • Stack traces are clearer
  • Adding logic later doesn't require restructuring
  • Expression bodies hide complexity in one line