Back to Devexpress

Inline Lambda

coderushforroslyn-401012-refactoring-assistance-inline-lambda.md

latest1.5 KB
Original Source

Inline Lambda

  • Aug 19, 2021
  • 2 minutes to read

Purpose

This refactoring creates a lambda expression and inlines a referenced delegate. If there is only one reference to a delegate method, the Inline Lambda refactoring deletes the delegate method. Use this Refactoring to create the delegate in a lambda syntax.

Availability

This refactoring is available when the caret is in a method reference within a delegate creation statement.

How to Use

  1. Place the caret in a method reference within a delegate creation statement. For example, in the “UserControl1_Loaded” method reference.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Inline Lambda from the menu.

After execution, this Refactoring creates a lambda expression and inlines the referenced delegate.

csharp
namespace Inline {   
    public partial class UserControl1 : UserControl {
        public UserControl1() {
            InitializeComponent();

            Loaded += (sender, e) =>
            {
                if (sender != null)
                    sender.ToString();
            };
        }
    }
}
vb
Namespace Inline
    Partial Public Class UserControl1
        Inherits UserControl

        Public Sub New()
            InitializeComponent()
            Loaded += Sub(sender, e) If sender IsNot Nothing Then sender.ToString()
        End Sub
    End Class
End Namespace