Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating objects in Loop C#

Tags:

c#

Just was curious about below code.

for (int i=0; i<9; i++)
{
   ClassA objectA = new ClassA();
}

or

ClassA objectA;
for (int i=0; i<9; i++)
{
    objectA = new ClassA();
}

Any Idea is there any difference for both code ? from my knowledge both will create different instances each time so number of instances are going to same .. Any idea ?

like image 380
batwadi Avatar asked Feb 23 '10 20:02

batwadi


People also ask

Can you create objects in a loop?

You CAN use a loop. The trick is that you have to save the reference to each one as you create it. A simple way would be to use an array. You have to declare the array outside the loop, then use your loop counter as the index into the array...

Can you create objects in a loop C++?

Creating objects in a loop is always valid. It is very common too. "I thought it wouldnt be possible because I would have a series of objects with the same name. " that won't be a problem because everytime it loops back, all objects within the loop scope will be destructed, so the object no longer exists.

Can we create object in for loop in Java?

Officially, no, all Java objects are instances of classes.


3 Answers

Scoping aside (i.e. whether the variable exists outside of the loop) there is usually no difference, since .NET variables are actually (in the IL) all at the start of the method anyway. However, there is an exception: if you capture the variable (into an anonymous method / lambda), then it behaves differently - the capture is constructed to respect the C# declaration. So:

List<Action> actions = new List<Action>();
ClassA objectA;
for (int i=0;i<9;i++)
{
    objectA= new ClassA();
    actions.Add(delegate { Console.WriteLine(objectA.GetHashCode()); });        
}
foreach(Action action in actions) action();

and:

List<Action> actions = new List<Action>();
for (int i=0;i<9;i++)
{
    ClassA  objectA= new ClassA();
    actions.Add(delegate { Console.WriteLine(objectA.GetHashCode()); });        
}
foreach(Action action in actions) action();

Will do different things (the first prints the same address-based hash-code 9 times; the second prints 9 different address-based hash-codes, indicating that in the second loop we've captured 9 different variables, instead of a single variable).

In both cases there are 9 ClassA objects created - simply that we can't see 8 of them any more in the first case.

like image 130
Marc Gravell Avatar answered Sep 22 '22 00:09

Marc Gravell


The only difference is that in the second example, the last created instance will still be accessible after the loop

like image 30
John Knoeller Avatar answered Sep 23 '22 00:09

John Knoeller


Actually, since you never save a reference to your objects that you create, each time you override objectA, you will have eliminated the previous example.

However, in the case of:

for (int i=0;i<9;i++)
{
   ClassA objectA = new ClassA();
}

objectA does not exist outside of the scope of the loop, and all of the objects you created will eventually be deleted by the garbage collector. It would be a syntax error to reference objectA outside of the loop.

In contrast, in the case of:

ClassA objectA;
for (int i=0;i<9;i++)
{
    objectA= new ClassA();
}

objectA is a valid identifier outside of the loop, and the final instance of ClassA created will remain after the loop is completed.

like image 34
David Pfeffer Avatar answered Sep 19 '22 00:09

David Pfeffer