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 91859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:05:45+00:00 2026-05-10T23:05:45+00:00

Should I instantiate my worker variables inside or outside my for loop E.g. a)

  • 0

Should I instantiate my worker variables inside or outside my for loop

E.g.

a)

bool b = default(bool);  for (int i = 0; i < MyCollection.Length; i++) {   b = false;    foreach(object myObject in myObjectCollection)   {     if (object.Property == MyCollection[i].Property)     {       b = true;       break;     }   }          if (b)   {     DoSomethingWith(MyCollection[i]);   } } 

b)

for (int i = 0; i < MyCollection.Length; i++) {   bool b = default(bool);    foreach(object myObject in myObjectCollection)   {     if (object.Property == MyCollection[i].Property)     {       b = true;       break;     }   }          if (b)   {     DoSomethingWith(MyCollection[i]);   } } 

EDIT: Seems universally agreed that there will be no difference where the IL is concerned. But for readability and clarity of scope… inside is better

  • 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-10T23:05:46+00:00Added an answer on May 10, 2026 at 11:05 pm

    Previous answer deleted as I’d misread the code. (Using ‘default(bool)’ anywhere is a bit odd, btw.)

    However, unless the variable is captured by a delegate etc, I’d expect them to either compile to IL which is effectively the same (in terms of both behaviour and performance).

    As ever, write the most readable code first. Micro-optimising things like this is asking for trouble. I agree with the others who have suggested that you restrict the scope of variables as much as you can – so if you need it after the loop, you haven’t got any choice anyway; otherwise declare it inside.

    Okay, here’s a test program:

    using System;  class Test {     static void Main() {}      static void DeclareInside()     {         for (int i=0; i < 10; i++)         {             bool x = false;             for (int j=5; j < 20; j++)             {                 if (i == j)                 {                     x = true;                     break;                 }                 if (x)                 {                     Console.WriteLine('Yes');                 }             }         }     }      static void DeclareOutside()     {         bool x;         for (int i=0; i < 10; i++)         {             x = false;             for (int j=5; j < 20; j++)             {                 if (i == j)                 {                     x = true;                     break;                 }                 if (x)                 {                     Console.WriteLine('Yes');                 }             }         }     } } 

    Generated IL (just csc Test.cs):

    .method private hidebysig static void  DeclareOutside() cil managed {   // Code size       79 (0x4f)   .maxstack  2   .locals init (bool V_0,            int32 V_1,            int32 V_2,            bool V_3)   IL_0000:  nop   IL_0001:  ldc.i4.0   IL_0002:  stloc.1   IL_0003:  br.s       IL_0045   IL_0005:  nop   IL_0006:  ldc.i4.0   IL_0007:  stloc.0   IL_0008:  ldc.i4.5   IL_0009:  stloc.2   IL_000a:  br.s       IL_0037   IL_000c:  nop   IL_000d:  ldloc.1   IL_000e:  ldloc.2   IL_000f:  ceq   IL_0011:  ldc.i4.0   IL_0012:  ceq   IL_0014:  stloc.3   IL_0015:  ldloc.3   IL_0016:  brtrue.s   IL_001d   IL_0018:  nop   IL_0019:  ldc.i4.1   IL_001a:  stloc.0   IL_001b:  br.s       IL_0040   IL_001d:  ldloc.0   IL_001e:  ldc.i4.0   IL_001f:  ceq   IL_0021:  stloc.3   IL_0022:  ldloc.3   IL_0023:  brtrue.s   IL_0032   IL_0025:  nop   IL_0026:  ldstr      'Yes'   IL_002b:  call       void [mscorlib]System.Console::WriteLine(string)   IL_0030:  nop   IL_0031:  nop   IL_0032:  nop   IL_0033:  ldloc.2   IL_0034:  ldc.i4.1   IL_0035:  add   IL_0036:  stloc.2   IL_0037:  ldloc.2   IL_0038:  ldc.i4.s   20   IL_003a:  clt   IL_003c:  stloc.3   IL_003d:  ldloc.3   IL_003e:  brtrue.s   IL_000c   IL_0040:  nop   IL_0041:  ldloc.1   IL_0042:  ldc.i4.1   IL_0043:  add   IL_0044:  stloc.1   IL_0045:  ldloc.1   IL_0046:  ldc.i4.s   10   IL_0048:  clt   IL_004a:  stloc.3   IL_004b:  ldloc.3   IL_004c:  brtrue.s   IL_0005   IL_004e:  ret } // end of method Test::DeclareOutside  .method private hidebysig static void  DeclareInside() cil managed {   // Code size       79 (0x4f)   .maxstack  2   .locals init (int32 V_0,            bool V_1,            int32 V_2,            bool V_3)   IL_0000:  nop   IL_0001:  ldc.i4.0   IL_0002:  stloc.0   IL_0003:  br.s       IL_0045   IL_0005:  nop   IL_0006:  ldc.i4.0   IL_0007:  stloc.1   IL_0008:  ldc.i4.5   IL_0009:  stloc.2   IL_000a:  br.s       IL_0037   IL_000c:  nop   IL_000d:  ldloc.0   IL_000e:  ldloc.2   IL_000f:  ceq   IL_0011:  ldc.i4.0   IL_0012:  ceq   IL_0014:  stloc.3   IL_0015:  ldloc.3   IL_0016:  brtrue.s   IL_001d   IL_0018:  nop   IL_0019:  ldc.i4.1   IL_001a:  stloc.1   IL_001b:  br.s       IL_0040   IL_001d:  ldloc.1   IL_001e:  ldc.i4.0   IL_001f:  ceq   IL_0021:  stloc.3   IL_0022:  ldloc.3   IL_0023:  brtrue.s   IL_0032   IL_0025:  nop   IL_0026:  ldstr      'Yes'   IL_002b:  call       void [mscorlib]System.Console::WriteLine(string)   IL_0030:  nop   IL_0031:  nop   IL_0032:  nop   IL_0033:  ldloc.2   IL_0034:  ldc.i4.1   IL_0035:  add   IL_0036:  stloc.2   IL_0037:  ldloc.2   IL_0038:  ldc.i4.s   20   IL_003a:  clt   IL_003c:  stloc.3   IL_003d:  ldloc.3   IL_003e:  brtrue.s   IL_000c   IL_0040:  nop   IL_0041:  ldloc.0   IL_0042:  ldc.i4.1   IL_0043:  add   IL_0044:  stloc.0   IL_0045:  ldloc.0   IL_0046:  ldc.i4.s   10   IL_0048:  clt   IL_004a:  stloc.3   IL_004b:  ldloc.3   IL_004c:  brtrue.s   IL_0005   IL_004e:  ret } // end of method Test::DeclareInside 

    The only differences are where the variables are located within the stack.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • 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 One way of doing this is to create a custom… May 11, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer The increase in load you're seeing is probably due to… May 11, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer Try setting "Ignore failure" on both errors and truncation. See:… May 11, 2026 at 11:32 pm

Related Questions

I'm trying to wrap my head around reflection, so I decided to add plugin
I have something similar to the following method: public ActionResult Details(int id) { var
While I realize that I could just show the form off-screen and hide it,
My current application is using an instance based data access layer. I instantiate the

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.