Refactorings for anonymous methods and lambda expressions
An anonymous method is a method without a name – which is why it is called anonymous. You don’t declare anonymous methods like regular methods. Instead, they get declared right inside a member as an inline code block. This is one of the greatest benefits of an anonymous method – it removes the requirement to create a named method elsewhere in the code, especially for simple operations. For example, you are able to pass an inline anonymous method as a delegate parameter, which means that you can use anonymous methods anywhere a delegate type is expected:
Lambda Expressions are simply an improvement to the syntax of anonymous methods. In other words, a lambda expression supersedes anonymous methods as a shorter way to write inline code. There is no longer a need to use the ‘delegate’ keyword, or provide the type of the method parameter. The type of a parameter can usually be inferred by the compiler from its use. The sample code above will look like this using lambda expressions:
The code looks much simpler and readable now. However, it takes some time to “acclimate” to this syntax. DevExpress Refactor! Pro and CodeRush Xpress has two pairs of refactorings, which are opposites of each other, and a couple of bonus refactorings for anonymous methods and lambda expressions. Before you apply a refactoring, you can see what your code will look like and learn the new syntax.
Pair #1 – conversion between anonymous methods and lambda expressions
- Compress to Lambda Expression
Converts an anonymous method into an equivalent lambda expression:
- The opposite: Expand Lambda Expression
Converts a lambda expression to an equivalent anonymous method:
Pair #2 – conversion between anonymous methods and regular methods
- Name Anonymous Method
Converts an anonymous method declaration that does not access local variables to a regular method declaration. The refactoring might be useful to make anonymous methods reusable in other locations. For example, when a single event handler is used for different events (especially when an events subscription code is located in different members):
Naming anonymous methods will result in the following code:
- The opposite: Inline Delegate
Converts a regular method into an anonymous method. If there are no other references to the delegate method, the method is removed.
Additional refactorings shipped in Refactor! Pro
- Introduce ForEach Action
Converts the generic List-iterating foreach loop into an anonymous method, which is passed as the Action delegate to the List.ForEach method. The ForEach method on a generic list performs the specified action on each element of the List. Consider the following code:
Here’s the preview hint for this refactoring:
and the resulting code:
- Remove Redundant Type Specification
Removes the redundant lambda parameter explicit type specification. The lambda expression may specify the type of its parameters explicitly, which is not required in most cases. The refactoring allows you to clean-up the lambda expression from the redundant type specification from this code:
to this:
You might also be interested in refactorings for anonymous types.
—– Products: Refactor! Pro, CodeRush Xpress Versions: 11.1 and up VS IDEs: any Updated: Sep/27/2011 ID: R041