What's the difference in memory between creating an object inside and outside of a loop

I want to clear up a few gaps in my knowledge. Starting with this.

Example (c#):

List<Person> names = new List<Person>();

Person friend = null;
for(int i = 0; i < 5; i++)
{
    friend = new Person();
    Person guy = new Person();

    guy.name = "Bob" + i;
    friend.name = "Bill" + i;

    names.Add(guy);
    names.Add(friend);
}

Here I have two Person objects used in the same loop just to save space.

It is my understanding that each time I instantiate friend, I reuse the same location in memory, overwriting the existing Person object, if present.

Whereas each new "guy" object is assigned a new location of memory.

Provided this is correct, and please correct me if this is wrong, this means that using "friend" over "guy" is more memory efficient.

Would there ever be a case where "guy" would be better? Could it depend on the implementation of the constructor?

Jon Skeet
people
quotationmark

It is my understanding that each time I instantiate friend, I reuse the same location in memory, overwriting the existing Person object, if present.

No, that's not the case. You're overwriting the previous value in the variable - but that's not the object. That's just a reference to the object. There's another reference to the object within the list. The object itself won't be overwritten at all. Each object you create is independent of the others.

In terms of the difference between declaring the variable inside the loop or outside the loop, it doesn't make much difference. There are a few differences:

  • If it's declared outside the loop, you can use the variable within the loop before assigning a new value to it, so you get to see the previous value
  • If it's declared outside the loop, you can use the variable after the loop as well, to read the last-assigned value
  • If it's declared outside the loop, and you use an anonymous function inside the loop, then every anonymous function will capture the same variable. If it's inside the loop, each anonymous function will capture a different variable. If all of this sounds like gobbledygook to you at the moment, you can probably ignore it
  • If it's declared inside the loop, then in a separate block you can declare a separate variable with the same name; you can't do that if it's declared outside the loop.

I'd generally recommend declaring variables with the minimum scope possible, at the point where you first need it - I find that ends up with clearer code.

people

See more on this question at Stackoverflow