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 ?
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...
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.
Officially, no, all Java objects are instances of classes.
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.
The only difference is that in the second example, the last created instance will still be accessible after the loop
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With