Skip to main content
All docs
V22.2

Expand Lambda Function

Expands the lambda function with an expression body onto the lambda function with a block body. You can use this code formatter when you need to extend the code logic in the lambda function and the expression body is not appropriate for this task.

Availability

Available when the caret is in a lambda operator (=>).

Usage

  1. Place the caret in a lambda operator (=>).

    using System;
    
    namespace ConsoleApp 
    {
        public class ExpandLambda  
        {
            Func<string, int> func = s => s == null ? 0 : s.Length;
        }
    }
    
  2. Press the Ctrl+. or Ctrl+~ shortcut to invoke the Code Actions menu.

  3. Select Expand Lambda Function from the menu and press Enter.

    menu

After execution, this code formatter expands a single-line lambda function onto a lambda function with a code block body.

using System;

namespace ConsoleApp {
    public class ExpandLambda  {
        Func<string, int> func = s => {
            return s == null ? 0 : s.Length;
        };
    }
}