All rules
IDE0200Language rules (code-block preferences)

Remove unnecessary lambda expression

Remove unnecessary lambda expression

Microsoft docs

Description

This rule flags the use of a lambda expression where it's unnecessary. Lambda expressions might be unnecessary when the following are all true:

  • The expression includes a method invocation.
  • The lambda expression has the same number and order of parameters as the method invocation.
  • The method invocation has no side effects.
  • The lambda expression isn't assigned to a non-delegate type.
  • If the invocation is a generic method, the type arguments are supplied.
  • The invoked method's return type can be converted to the lambda expression's return type.
  • There's only one applicable method in the method group.

Example

// Code with violations.
bool IsEven(int x) => x % 2 == 0;
_ = new[] { 1, 2, 3 }.Where(n => IsEven(n));

// Fixed code.
bool IsEven(int x) => x % 2 == 0;
_ = new[] { 1, 2, 3 }.Where(IsEven);

Configurable options

Vote for the value each option should take in the generated .editorconfig.

csharp_style_prefer_method_group_conversion
default: true
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0