CS.UNINIT.LOOP_COUNTER
Uninitialized loop counter in for statement.
Loop counter not declared in initialization expression in for statement.
Vulnerability and risk
Unless there is an explicit reason, counter variables should be declared and initialized in the initialization expression of a for statement, to prevent unintended loop counter reuse.
Mitigation and prevention
Unless there is an explicit reason, counter variables are declared and initialized in the for statement's initialization expression.
Example
1 namespace kmcustom 2 { 3 class C05 4 { 5 public void testOK() 6 { 7 for (int i = 0; i < 5; i++) 8 { 9 //do something 10 } 11 } 12 13 public void testNG() 14 { 15 int i = 0; 16 for (i = 0; i < 5; i++) //NG 17 { 18 //do something 19 } 20 21 } 22 public void testNG2() 23 { 24 int i = 0; 25 for (; i < 5; i++) //NG 26 { 27 //do something 28 } 29 30 } 31 32 33 } 34 }