JetBrains Rider 2024.1 Help

Code Inspection: Local function can be made static

Category

Common Practices and Code Improvements

ID

LocalFunctionCanBeMadeStatic

EditorConfig

resharper_local_function_can_be_made_static_highlighting

Default severity

Disabled

Language

C#

Requires SWA

No

Static local functions are designed to be independent of the state of the enclosing scope. Therefore, if JetBrains Rider detects a local function that does not use any variables from the enclosing scope, it suggests making such function static.

This way JetBrains Rider infers the intended use of the function and makes sure that this intent is clearly visible to those who will read or modify the code later.

class Test { void Foo() { Console.WriteLine(LocalFunction1("test ")); // Can be made static because no local variables are captured string LocalFunction1(string str) => str.Trim(' '); const string test = "test "; Console.WriteLine(LocalFunction2()); // Cannot be made static because the variable 'test' is captured string LocalFunction2() => test.Trim(' '); } }
Last modified: 15 April 2024