tags:

views:

212

answers:

6
....
....
Employee employeeInfo;
for(int i =0; i<n;++i)
{
   employeeInfo = new Employee();
   employeeInfo.FirstName = arr[i].ToString();
   employeeInfo.Age = i;
   employeeList.Add(employeeInfo);
    .... 
}
A: 

That all depends what code you have hidden with your .... ....!

David M
A: 

According to your code snippet:

in every iteration you create a new object and previous object, referenced by employeeInfo will be eligible for GC.

Upul
Or the constructor of Employee must refer to some static collection... But that would be really ugly, but the topic Starter's snippet doesn't seem useful either...
Dykam
A: 

If you don't do anything else with the employeeInfo you created then in means you are doing something useless - create objects that are not used => lost time allocating memory

Victor Hurdugaci
+4  A: 

Objects doesn't have names. What you have is a local variable that is a reference to an object, and the variable is used for each object that is created.

You can use the local variable to keep track of the current object, and later in the loop store the object in a collection.

If you don't store each object somewhere, the previous object will be lost when you assign the next object to the variable. The previous object still exists, but as there is no longer any reference to it, it will be removed by the garbage collector later on.

Guffa
@Guffa,if I've understood your explanations,you mean that when creating a new employee object,there will be a variable in the stack and an object in heap.Every iteration that program instantiates a new employee object,a new object in the heap in different location (with different address) will be created but we have only one variable called employeeInfo in the stack.after setting value to properties,we add the created object in a list.In fact, we store the address of the object somewhere.Right?
odiseh
@Guffa:There is a qquestion:As all we know, GC action's nondeterministic.Suppose n = 10,i = 8, so we 've added 8 objects to our list till now, or we've stored 8 addresses in the list. If suddenly GC starts its action now it will clear the not used objects, So what will happen to our addresses?
odiseh
@odiseh: The garbage collector only removes objects that is unreachable. Removing a reference to an object doesn't automatically cause it to be collected. As long as you have references to the objects in the list, they are safe.
Guffa
A: 
employeeInfo = new Employee();//this is where you are "instantiating objects with the same name" in your for loop. 

Everytime when this loc is called, your previous value in employeeInfo object gets replaced with the initial state of the object; this means when this new is called, then properties/variables of employeeInfo shall be replaced by default values(provided by constructor, if any).

KMan
A: 

See the variable as a box. You place a new Employee() in the box, give it a name, add it to some collection. On he next iteration of the loop you place turn over the box(throwing away the reference to the previous Employee, but that doesnt matter since your collection still has the reference) and add yet a new Employee() in the box. Anything not in a box will be cleaned by the garbage collector

gavit