BRO1136: A lambda's single parameter should not be in parentheses¶
A lambda with one parameter that has no type, modifier or attribute is written x => ..., not (x) => .... Not a
StyleCop rule (proposed in StyleCop issue #762).
Example¶
Before¶
using System.Collections.Generic;
using System.Linq;
public class Orders
{
public IEnumerable<int> Large(IEnumerable<int> amounts) => amounts.Where((amount) => amount > 100);
}
After¶
using System.Collections.Generic;
using System.Linq;
public class Orders
{
public IEnumerable<int> Large(IEnumerable<int> amounts) => amounts.Where(amount => amount > 100);
}
Why¶
The parentheses add nothing for a single untyped parameter, and most code (and the SDK's own code generation) leaves them out. One form is easier to scan.
How the fix works¶
The parameter list (x) is replaced with x; async and static in front stay.
Not reported¶
- No parameter or several:
() => 1,(x, y) => x + y. - A typed parameter, a modifier or a default value:
(int x) => x,(ref int x) => x++. - Attributes on the parameter or the lambda, and an explicit return type:
([A] x) => x,[A] (x) => x,int (x) => x(they need the parentheses). - A comment inside the parentheses.
Compared with StyleCop¶
StyleCop has no such rule; a maintainer suggested folding it into SA1119 (BRO1405), which never happened.
stylebro-migrate turns it off (see
Rules beyond StyleCop).