Skip to main content

Use Expression Body

Purpose

Converts a property, method, constructor, destructor, getter or setter into the expression-bodied structure.

Availability

Available in the following cases.

  • When the cursor is on the name of a method, constructor, destructor, getter or setter in its declaration, assuming that it contains a single statement.
  • When the cursor is on the property name in its declaration, assuming that the property get accessor contatins a single return statement and the property has no set accessor.

Usage

  1. Place the caret on a method name.

    Note

    The blinking cursor shows the caret’s position at which the Refactoring is available.

    double ConvertToUSD(Currency source, double amount) {
        return amount / GetCurrencyRate(source);
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Use Expression Body from the menu.

After execution, the Refactoring converts the traditional method declaration to the expression-bodied structure.

double ConvertToUSD(Currency source, double amount) => amount / GetCurrencyRate(source);

Note

This feature is available as a part of Code Cleanup.

See Also