All rules
IDE0320Language rules (modifier preferences)

Make anonymous function static

Make anonymous function static

Microsoft docs

Description

This style rule flags anonymous functions that can be marked static. Marking them as static prevents local variables from being captured, which would result in a memory allocation.

Example

// Code with violations.
M(x => x + 1);
M(delegate (int x) { return x + 1; });

void M(Func<int, int> f) { }

// Fixed code.
M(static x => x + 1);
M(static delegate (int x) { return x + 1; });

void M(Func<int, int> f) { }

Configurable options

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

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