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

I wanted to find out if it is possible to call MS Access Stored
I wanted to find out how to specify fields to appear side-by-side in BIRT
I am using SQL Server 2008. I have tables on which there are duplicate
So I'm running out of time on this project and I posted here in
How does a .NET decimal type get represented in binary in memory? We all
My instructor gave us example code that is very similiar to the code below.
Question: Today I worked with MS Visual Sourcesafe, that is to say Microsoft's Sourcecode
I am developing a schema using RELAX NG. I'm pretty new to this, so
I want to use a linux command line tool from my Java program. I
I have got myself saddled with an existing application - which attempts do do

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.