Skip to main content

Collapse/Expand Local Function

  • 2 minutes to read

Collapses a local function body onto a single line and vice versa. Single-line local functions make your code compact.

Availability

Available when the caret is at the beginning of a local function declaration or in the local function name. The local function body should consist of a single statement.

How to Use

  1. Place the caret in the name of a local function. The local function body should contain a single statement.

    Note

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

class Project {
    int Fibonachi(int n) {
        if (n < 0)
            throw new ArgumentOutOfRangeException(nameof(n), "n must be greater than or equal to zero.");
        return Fib(n);

        int Fib(int n) {
            return n < 2 ? n : Fib(n - 1) + Fib(n - 2);
        }
    }
}
  1. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  2. Select Collapse Local Function (Expand Local Function if you want to expand a local function) from the menu.

    Formatting_Spaces

After execution, the Code Formatter removes the line breaks and converts the local function to a single-line form or expands the local function to a multiple-line form.

The code below shows the collapsed local function.

class Project {
    int Fibonachi(int n) {
        if (n < 0)
            throw new ArgumentOutOfRangeException(nameof(n), "n must be greater than or equal to zero.");
        return Fib(n);

        int Fib(int n) { return n < 2 ? n : Fib(n - 1) + Fib(n - 2); }
    }
}