ReSharper 2024.1 Help

Code Inspection: Empty 'for' loop is redundant

Category

Redundancies in Code

ID

EmptyForStatement

EditorConfig

resharper_empty_for_statement_highlighting

Default severity

Warning

Language

C#

Requires SWA

No

If a for loop contains no instructions, and its initialization, condition, and iteration do not use variables from the outer scope, ReSharper suggests removing this loop, for example:

public void MyMethod() { for (int i=0; i < 100; i++) { } }

Note, however, that a for loop with an empty body could be actually used to iterate through external variables, in which case ReSharper will not highlight it as redundant, for example:

public void MyMethod() { int a; var b = 0; for (a=0; a < 100; b += a++) { } Console.WriteLine("b = " + b); }
Last modified: 15 April 2024