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

  • Home
  • SEARCH
  • 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 6554231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:40:31+00:00 2026-05-25T12:40:31+00:00

Is the following: MyObject myVariable; for(int i = 0; i < objects.Length, i++){ myVariable

  • 0

Is the following:

MyObject myVariable;
for(int i = 0; i < objects.Length, i++){
  myVariable = objects[i];
  // do stuff...
}

more efficient then:

for(int i = 0; i < objects.Length, i++){
  MyObject myVariable = objects[i];
  // do stuff...
}

because a new variable to hold a reference is not created every time? (or is the complier intelligent enough just to use the same variable)..

(If a new variable is created is it malloced on the heap?)

  • 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-25T12:40:32+00:00Added an answer on May 25, 2026 at 12:40 pm

    No, "variables" exist almost entirely for the sake of the programmer. You’re not creating any additional work at run-time by declaring the variable inside the method.

    In theory, the compiler will set aside space on the stack when a method is called for each variable declared in that method. So the presence of that variable in the method would be more important than its scope. No space is allocated on the heap unless the new keyword is used.

    In practice, the compiler can identify variables that have such a short scope that they can be stored in a register on the CPU instead of needing space on the stack. For example:

    var a = b[c];
    a.ToString();
    // never access "a" again.
    

    … would be the same as:

    b[c].ToString();
    

    … because the compiler recognizes that it only needs to store the result of b[c] long enough to call a method on it, so it can just use a CPU register instead of using memory.

    For this reason, declaring your variable inside the loop could actually cause the method to allocate less stack space for the variable, depending on the possible logic flow afterward. However, that gets into huge micro-optimization that doesn’t make any sense for most people to care about.

    Update

    Since some people still seem to think that declaring a variable in a loop has some effect, I guess I need to provide proof. Type the following programs into LINQPad.

    int j;
    for(int i = 0; i < 5; i++)
    {
        j = i;
    }
    

    … and…

    for(int i = 0; i < 5; i++)
    {
        int j = i;
    }
    

    Execute the code, then go to the IL tab to see the generated IL code. It’s the same for both of these programs:

    IL_0000:  ldc.i4.0    
    IL_0001:  stloc.0     
    IL_0002:  br.s        IL_0008
    IL_0004:  ldloc.0     
    IL_0005:  ldc.i4.1    
    IL_0006:  add         
    IL_0007:  stloc.0     
    IL_0008:  ldloc.0     
    IL_0009:  ldc.i4.5    
    IL_000A:  blt.s       IL_0004
    

    So there’s incontrovertible proof that this will make no difference at compile time. You’ll get exactly the same compiled IL from both programs.

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

Sidebar

Related Questions

Say we have the following method: private MyObject foo = new MyObject(); // and
Lets say I have the following: public class MyObject { [Bindable] public var foo:int
What is the best way to do the following: List<MyObject> list = new LinkedList<MyObject>();
I have the following eval(id+' = new MyObject()'); and I want to access the
I have an NSArray of objects: MyObject consists of the following string properties: Title
Say I have the following code: var album = new MyObject('album'); Assume that when
I do the following, and it evaluates to false : MyObject.new.class === MyObject However,
I have the following code MyObject * func1() { MyObject * obj = new
I have the following code: var json = MyObject .Select(p => new { id
I am trying to create an expression tree that represents the following: myObject.childObjectCollection.Any(i =>

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.