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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:14:34+00:00 2026-05-17T16:14:34+00:00

Is it faster to declare variables inside a loop or outside a loop? For

  • 0

Is it faster to declare variables inside a loop or outside a loop? For example:

' Declaration inside of the loop
For each item in items
    Dim newVariable as String = GetAString()
Next

' Declaration outside of the loop
Dim newVariable as String = String.Empty
For each item in items
    newVariable = GetAString()
Next

Which one is faster? Why? I assume the latter is faster because it is just reusing the same “pointer” to reference a new value behind the scenes instead of creating a new pointer each iteration, correct? Can someone elaborate?

Thanks

Updated:

The Compiler is intelligent enough to optimize the code when generating the Intermediate Language. It moves the variable declarations to the top of the method. Below, is the declartions within the IL after compilation:

 .locals init ([0] string newVariable2,
           [1] int32 i,
           [2] string newVariable,
           [3] int32 V_3,
           [4] int32 VB$CG$t_i4$S0)

Here’s the entire IL for those interested:

.method private instance void  Form1_Load(object sender,
                                          class [mscorlib]System.EventArgs e) cil managed
{
  // Code size       55 (0x37)
  .maxstack  2
  .locals init ([0] string newVariable2,
           [1] int32 i,
           [2] string newVariable,
           [3] int32 V_3,
           [4] int32 VB$CG$t_i4$S0)
  IL_0000:  nop
  IL_0001:  ldc.i4.0
  IL_0002:  stloc.1
  IL_0003:  ldarg.0
  IL_0004:  callvirt   instance string WindowsApplication1.TestVariableDeclaration::getstring()
  IL_0009:  stloc.2
  IL_000a:  nop
  IL_000b:  ldloc.1
  IL_000c:  ldc.i4.1
  IL_000d:  add.ovf
  IL_000e:  stloc.1
  IL_000f:  ldloc.1
  IL_0010:  ldc.i4     0x989680
  IL_0015:  stloc.s    VB$CG$t_i4$S0
  IL_0017:  ldloc.s    VB$CG$t_i4$S0
  IL_0019:  ble.s      IL_0003
  IL_001b:  ldc.i4.0
  IL_001c:  stloc.3
  IL_001d:  ldarg.0
  IL_001e:  callvirt   instance string WindowsApplication1.TestVariableDeclaration::getstring()
  IL_0023:  stloc.0
  IL_0024:  nop
  IL_0025:  ldloc.3
  IL_0026:  ldc.i4.1
  IL_0027:  add.ovf
  IL_0028:  stloc.3
  IL_0029:  ldloc.3
  IL_002a:  ldc.i4     0x989680
  IL_002f:  stloc.s    VB$CG$t_i4$S0
  IL_0031:  ldloc.s    VB$CG$t_i4$S0
  IL_0033:  ble.s      IL_001d
  IL_0035:  nop
  IL_0036:  ret
} // end of method TestVariableDeclaration::Form1_Load
  • 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. Editorial Team
    Editorial Team
    2026-05-17T16:14:35+00:00Added an answer on May 17, 2026 at 4:14 pm

    I agree with Kevin’s answer, define variables where they have meaning. Worry about optimizations if and when they present themselves and you know that a variable declaration is the issue. However, consider the following two pieces of code

    void Test1()
    {
        foreach (int i in Enumerable.Range(0,10))
        {
            string s = GetString();
            Console.WriteLine(s);
        }
    }
    
    void Test2()
    {
        string s;
        foreach (int i in Enumerable.Range(0,10))
        {
            s = GetString();
            Console.WriteLine(s);
        }
    }
    

    And their generated IL:

    Test1:
    IL_0000:  ldc.i4.0    
    IL_0001:  ldc.i4.s    0A 
    IL_0003:  call        System.Linq.Enumerable.Range
    IL_0008:  callvirt    System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator
    IL_000D:  stloc.1     
    IL_000E:  br.s        IL_0024
    IL_0010:  ldloc.1     
    IL_0011:  callvirt    System.Collections.Generic.IEnumerator<System.Int32>.get_Current
    IL_0016:  pop         
    IL_0017:  ldarg.0     
    IL_0018:  call        UserQuery.GetString
    IL_001D:  stloc.0     
    IL_001E:  ldloc.0     
    IL_001F:  call        System.Console.WriteLine
    IL_0024:  ldloc.1     
    IL_0025:  callvirt    System.Collections.IEnumerator.MoveNext
    IL_002A:  brtrue.s    IL_0010
    IL_002C:  leave.s     IL_0038
    IL_002E:  ldloc.1     
    IL_002F:  brfalse.s   IL_0037
    IL_0031:  ldloc.1     
    IL_0032:  callvirt    System.IDisposable.Dispose
    IL_0037:  endfinally  
    IL_0038:  ret         
    
    Test2:
    IL_0000:  ldc.i4.0    
    IL_0001:  ldc.i4.s    0A 
    IL_0003:  call        System.Linq.Enumerable.Range
    IL_0008:  callvirt    System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator
    IL_000D:  stloc.1     
    IL_000E:  br.s        IL_0024
    IL_0010:  ldloc.1     
    IL_0011:  callvirt    System.Collections.Generic.IEnumerator<System.Int32>.get_Current
    IL_0016:  pop         
    IL_0017:  ldarg.0     
    IL_0018:  call        UserQuery.GetString
    IL_001D:  stloc.0     
    IL_001E:  ldloc.0     
    IL_001F:  call        System.Console.WriteLine
    IL_0024:  ldloc.1     
    IL_0025:  callvirt    System.Collections.IEnumerator.MoveNext
    IL_002A:  brtrue.s    IL_0010
    IL_002C:  leave.s     IL_0038
    IL_002E:  ldloc.1     
    IL_002F:  brfalse.s   IL_0037
    IL_0031:  ldloc.1     
    IL_0032:  callvirt    System.IDisposable.Dispose
    IL_0037:  endfinally  
    IL_0038:  ret   
    

    See any difference? Those compiler guys, they’re smart.

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

Sidebar

Related Questions

No related questions found

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.