title: Declaring variables within FOR loops
body: <p>A weird bug was occurring in production which I was asked to look into.<br/> The issue was tracked down to a couple of variables being declared within a For loop and not being initialized on each iteration. An assumption had been made that due to the scope of their declaration they would be &quot;reset&quot; on each iteration. <br/>Could someone explain why they would not be)?<br />
  (My first question, really looking forward to the responses.)<br/>
  The example below is obviously not the code in question but reflects the scenario: <br/>
  Please excuse the code example, it looks fine in the editor preview??</p>
  <pre><code>for (int i =0; i&lt; 10; i++)
  {
      decimal? testDecimal;
      string testString;
  
      switch( i % 2  )
      {
          case 0:
          testDecimal = i / ( decimal ).32;
          testString = i.ToString();
              break;
          default:
              testDecimal = null;
              testString = null;
              break;
      }
  
      Console.WriteLine( &quot;Loop {0}: testDecimal={1} - testString={2}&quot;, i, testDecimal , testString );
  }
  </code></pre>
  <hr />
  <h3>EDIT:</h3>
  <p>Sorry, had to rush out for child care issue. The issue was that the prod code had was that the switch statement was huge and in some &quot;case&quot;'s a check on a class' property was being made, like if (myObject.Prop != null) then testString = myObject.Stringval... At the end of the switch, (outside) a check on testString == null was being made but it was holding the value from the last iteration,hence not being null as the coder assumed with the variable being declared within the loop.<br/> Sorry if my question and example was a bit off, I got the phone call about the day care as I was banging it together. I should have mentioned I compared IL from both variables in and out the loop. So, is the common opinion that &quot;obviously the variables would not be reinitialized on each loop&quot;?<br>
  A little more info, the variables WHERE being initialized on each iteration until someone got over enthusiastic with ReSharper pointing out &quot;the value is never used&quot; and removed them.</p>
  <hr />
  <h3>EDIT:</h3>
  <p>Folks, I thank you all. As my first post I see how much clearer I should be in the future. The cause of our unexpected variable assignment can me placed on an inexperienced developer doing everything ReSharper told him and not running any unit tests after he ran a &quot;Code Cleanup&quot; on an entire solution. Looking at the history of this module in VSS I see variables Where declared outside of the loop and where initialized on each iteration. The person in question wanted his ReSharper to show &quot;all green&quot; so &quot;moved his variables closer to assignment&quot; then &quot;Removed redundant assignment&quot;! I don't think he will be doing it again...now to spend the weekend running all the unit tests he missed!<br/> How to do mark a question as answered?</p>