Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 90643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:54:19+00:00 2026-05-10T22:54:19+00:00

A weird bug was occurring in production which I was asked to look into.

  • 0

A weird bug was occurring in production which I was asked to look into.
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 "reset" on each iteration.
Could someone explain why they would not be)?
(My first question, really looking forward to the responses.)
The example below is obviously not the code in question but reflects the scenario:
Please excuse the code example, it looks fine in the editor preview??

for (int i =0; i< 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( "Loop {0}: testDecimal={1} - testString={2}", i, testDecimal , testString ); } 

EDIT:

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 "case"’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.
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 "obviously the variables would not be reinitialized on each loop"?
A little more info, the variables WHERE being initialized on each iteration until someone got over enthusiastic with ReSharper pointing out "the value is never used" and removed them.


EDIT:

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 "Code Cleanup" 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 "all green" so "moved his variables closer to assignment" then "Removed redundant assignment"! I don’t think he will be doing it again…now to spend the weekend running all the unit tests he missed!
How to do mark a question as answered?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:54:20+00:00Added an answer on May 10, 2026 at 10:54 pm

    Most of the time, it does not matter whether you declare a variable inside or outside the loop; the rules of definite assignment ensure that it doesn’t matter. In the debugger you might occasionally see old values (i.e. if you look at a variable in a breakpoint before it is assigned), but static-analysis proves that this won’t impact executing code. The variables are never reset per loop, as there is demonstrably no need.

    At the IL level, **usually* the variable is declared just once for the method – the placement inside the loop is just a convenience for us programmers.

    HOWEVER there is an important exception; any time a variable is captured, the scoping rules get more complex. For example (2 secs):

            int value;         for (int i = 0; i < 5; i++)         {             value = i;             ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(value); });         }         Console.ReadLine(); 

    Is very different to:

            for (int i = 0; i < 5; i++)         {             int value = i;             ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(value); });         }         Console.ReadLine(); 

    As the ‘value’ in the second example is truly per instance, since it is captured. This means that the first example might show (for example) ‘4 4 4 4 4’, where-as the second example will show 0-5 (in any order) – i.e. ‘1 2 5 3 4’.

    So: were captures involved in the original code? Anything with a lambda, an anonymous method, or a LINQ query would qualify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Normally the OS an application is built on with Delphi… May 11, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer ANIZ in orgANIZation is not a complete word -- it's… May 11, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer KISS. If you don't have to call a constructor, even… May 11, 2026 at 4:33 pm

Related Questions

I have a weird error in my C++ classes at the moment. I have
I've just finished a six hour debugging session for a weird UI effect where
I'm getting a weird behaviour in my blog, only in Google Chrome. (This is
I read what-payment-structure-do-you-use-for-small-projects and I wonder how you guys are dealing with bug vs.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.