jeudi 8 juin 2017

C# Create New Objects vs Overwrite Existing Object (Performance)

I have to create a huge amount of slightly different objects for testing. All have their properties altered to be distinct object, but once I create the object & do something with it, I no longer need it. Should I create a new local object or, just reassign the object that exists outside the loop?

New (local) objects

int testCases = 10;
for(int i=0; i<testCases; i++){
    TestObject localObject = new TestObject();  //local variable for the loop
    //change properties & do stuff with the test object
}

Reassign Existing object

TestObject myObject;
int testCases = 10;
for(int i=0; i<testCases; i++){
    myObject = new TestObject();    //reassign object that exists outside the loop
    //change properties & do stuff with the test object
}

The "change properties & do stuff" isn't that important, I know how to create the different versions of my object, just not sure if I should be destroying them each iteration

Aucun commentaire:

Enregistrer un commentaire