ReSharper Code Cleanup does not fix “Convert to Lambda Expression” issues.


ReSharper Code Cleanup does not fix “Convert to Lambda Expression” issues.


Use regular expression find and replace to correct.


I am a recent ReSharper convert so my code does not match the format suggested by ReSharper.  The Code Cleanup feature of ReSharper can really help to get your code in line. However, there does not appear to be an option to have it convert code to Lambda Expressions for you.

Because I had so many lines of code to fix I decided to use the Regular Expression option of the Find and Replace dialog to fix all my lines at once.  The goal of a regular expression is to allow you to describe patterns of interest.

Below is an example of a line of code that would cause ReSharper to suggest changing to a Lambda Expression.

dest.DeviceGet = () => { return diverter; };


After being converted to an expression the results would appear like this:

dest.DeviceGet = () => diverter;

As you can see the curly braces, semicolon and return statement were removed. If you review the lines you will realize there is a pattern we can use to locate the lines that need to be corrected.

We are looking for a line that contains a curly brace ‘{‘ followed by a space ‘ ‘ and the word ‘return’ and another space ‘ ‘.

Now we want to copy everything up to the ‘;’. This portion of the line will remain after it is converted. Finally we want to select the ‘;’, space ‘ ‘ and closing curly brace ‘}’;  The regular expression to select this line is below:

\{ return {[^;]+}; \}

The regular expression begins be locating the starting curly brace \{. I must precede the curly brace with a backslash because it is a reserved character.

Next is the space followed by the word ‘return’ and another space.

Next we have to identify the portion of the string we need to copy. We do this by using a pair of curly braces {} to tag that portion of the string. Any pattern matched within those braces will be tagged so we can use it in the replace textbox of the Find and Replace dialog.

The next portion of our regular expression needs to match all characters up to but not including the first semicolon ‘;’.  We achieve this by adding “[^;]+” to our regular expression.  The square brackets define a character class.  A character class matches any character within the brackets.  However, if the first character is a circumflex ‘^’ it changes the meaning to match any character except the ones within the brackets.  Adding the + matches one or more occurrences of the preceding regular expression.  Because we places our character class within curly braces the matched string will be tagged for later use.

Finally we match the actual ‘;’, space and closing curly brace with “; \}”.

The completed regular expression defines what we want to find and tags the portion we need to retain.

Each pair of curly braces is given a number staring from 1. To use the first tagged portion in the Find and Replace dialog we simply type \1.  Below is a screen shot of the Find an Replace dialog ready to correct all your “Convert to Lambda Expression” warnings.  Note: you must check the “Use Regular expressions” check box.

Comments are closed