Erik Movsesyan

Erik Movsesyan

  • 2.1k
  • 9
  • 240

Object being created and destroyed in a forloop

May 14 2022 8:19 PM

Hello, I have the following code here:

class uno {
    int x;

    public uno(int i) {
        x = i;
    }

    public void gen(int h) {
        new uno(h);
    }
}

class dos {
    public static void Main() {
        uno jj = new(0);

        for (int i=1;i<=10;i++) {
            jj.gen(i);
        }
    }
}

 

I read in the following book: memberfiles.freewebs.com/02/83/78118302/documents/McGraw.Hill.CSharp.4.0.The.Complete.Reference.Apr.2010.pdf

pages 134-135

that the gen(int i); method creates and destroys objects immediately.

Does this mean that in a loop whwnwver you create objects after each itertion, the objetc is created and detroyed immediately?
If yes, then how is that object destroyed(obviously not by the GC as It says that the object is destroyed IMMEDIATELY)?

 


Answers (4)