Skip to content

use-lambda

C#stylefix: safedefault: offcsharp/stylecopcsharp/roslynator

Reports an anonymous method (delegate (x) { ... }) where a lambda can be written. The fix is offered when the anonymous method has a parameter list and no comment before its body; delegate { } without a parameter list converts to delegate types with any parameters, so it has no fix.

Lambdas are the short and usual form, and they bind the same way for the common cases. One form for all code avoids the extra keyword and makes the file read the same.

Reported
...
int a, b;
...
void F(object o, int i) {
var f = delegate (int x) { return x; };
...
}
...
After the fix
...
int a, b;
...
void F(object o, int i) {
var f = (int x) => { return x; };
...
}
...
csharp:
lint:
rules:
use-lambda: warn # off (default), warn, error

Enabled by the presets: csharp/stylecop, csharp/roslynator.

Suppress one line with sudsy-disable-next-line use-lambda in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
Roslynator RCS1048 full
StyleCop SA1130 full