All docs
V21.1
21.1
20.2
20.1
19.2
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

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); }
    }
}