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?
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:
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.