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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:35:28+00:00 2026-05-21T21:35:28+00:00

I wanted to find out what more efficient (if even) in the following code

  • 0

I wanted to find out what more efficient (if even) in the following code
Value type

ForEach(string s in strings)
{
  string t = s;
}
// or
string t;
ForEach(string s in strings)
{
   t = s;
}

and is there a different with reference types.

  • 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-21T21:35:28+00:00Added an answer on May 21, 2026 at 9:35 pm

    The two code snippets produce exactly the same IL. Here’s the C# code I used to test with:

      public string[] strings = new string[5];
    
      public void TestOne()
      {
         foreach (string s in strings)
         {
            string t = s;
         }
      }
    
      public void TestTwo()
      {
         string t;
         foreach (string s in strings)
         {
            t = s;
         }
      }
    

    And here’s the resulting IL for both methods after compiling with optimizations enabled:

    .method public hidebysig instance void  TestOne() cil managed
    {
      // Code size       26 (0x1a)
      .maxstack  2
      .locals init ([0] string s,
               [1] string[] CS$6$0000,
               [2] int32 CS$7$0001)
      IL_0000:  ldarg.0
      IL_0001:  ldfld      string[] strings
      IL_0006:  stloc.1
      IL_0007:  ldc.i4.0
      IL_0008:  stloc.2
      IL_0009:  br.s       IL_0013
      IL_000b:  ldloc.1
      IL_000c:  ldloc.2
      IL_000d:  ldelem.ref
      IL_000e:  stloc.0
      IL_000f:  ldloc.2
      IL_0010:  ldc.i4.1
      IL_0011:  add
      IL_0012:  stloc.2
      IL_0013:  ldloc.2
      IL_0014:  ldloc.1
      IL_0015:  ldlen
      IL_0016:  conv.i4
      IL_0017:  blt.s      IL_000b
      IL_0019:  ret
    } // end of method TestOne
    
    
    .method public hidebysig instance void  TestTwo() cil managed
    {
      // Code size       26 (0x1a)
      .maxstack  2
      .locals init ([0] string s,
               [1] string[] CS$6$0000,
               [2] int32 CS$7$0001)
      IL_0000:  ldarg.0
      IL_0001:  ldfld      string[] strings
      IL_0006:  stloc.1
      IL_0007:  ldc.i4.0
      IL_0008:  stloc.2
      IL_0009:  br.s       IL_0013
      IL_000b:  ldloc.1
      IL_000c:  ldloc.2
      IL_000d:  ldelem.ref
      IL_000e:  stloc.0
      IL_000f:  ldloc.2
      IL_0010:  ldc.i4.1
      IL_0011:  add
      IL_0012:  stloc.2
      IL_0013:  ldloc.2
      IL_0014:  ldloc.1
      IL_0015:  ldlen
      IL_0016:  conv.i4
      IL_0017:  blt.s      IL_000b
      IL_0019:  ret
    } // end of method TestTwo
    

    As usual, the rule is the same: trust your compiler. Let it handle these optimizations for you, rather than trying to worry about it as you write the code. Just write the code that makes sense and is readable.

    Most importantly, ignore all the people who argue that one of them is “theoretically” better than the other. That’s pure nonsense. There’s no theory that’s relevant until after the code is compiled. And since it compiles down to the exact same thing, it’s guaranteed to be completely identical in performance.

    But if you still don’t believe my urgings to trust your compiler (some of us are stubborn, I know because even I still am sometimes), at least you now know how to answer these types of questions on your own. Write some sample code that demonstrates both variations, compile it in “Release” mode, then open up the assembly in ILDASM.exe and compare the resulting IL code for yourself. Things like this are so easy to test, there’s never a reason to guess.

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

Sidebar

Related Questions

Hey I just wanted to find out the working of the following script as
I decided to learn concurrency and wanted to find out in how many ways
This is a common scenario, but i wanted to find out which way is
I'm not sure if this is possible at all, but wanted to find out
I know I could use PHP to do this, but wanted to find out
I am converting fortran code to C++ and wanted to find a right option
For instance, if I wanted to a find and replace with strings containing backward
I'm about to create some complex Ant build files and I wanted find out
Is there a t-sql script to find out tables that aren't being used in
I have a generator and would like to find out what the first value

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.