Code inspection: Move variable declaration inside loop condition
This inspection reports a variable declaration placed outside a loop even though it is only used for an assignment inside the loop condition. The quick-fix rewrites assignment-based null checks into pattern-based declarations directly in the loop condition.
Example
object o;
while ((o = NextObject()) != null)
{
Console.WriteLine(o);
}
while (NextObject() is { } o)
{
Console.WriteLine(o);
}
Quick-fix
Move the declaration into the loop condition and use a pattern declaration there.
30 March 2026