pokoko222
pokoko222

Reputation: 41

Destructor for object that has a member from a linked list

I have a Polygon class that has one Vertex member. That member is part of a linked list of vertices, those are the vertices of the polygon. So, the Polygon object contains just one reference to some of his vertices, and I get the others by moving through the linked list.

The problem is: Destroying a Polygon object. I come from c++ and what I do there in the destructor is begin with the vertex of the polygon, move through the list and delete all the vertices. What should I do in c#? First of all there is no delete, and there is automatic garbage collection so I don't know what to do.

This is the c++ Destructor explained:

    Polygon::~Polygon(void) {
    if (_v) { // _v is Vertex member the Polygon has, the only one
    Vertex *w = _v->cw(); 
    while (w != _v) { // advance through the linked list members and delete them
        delete w->remove();
        w = _v->cw();
    }
    delete _v; // finally delete the vertex which is member of the polygon
    }

Thank you

Upvotes: 0

Views: 212

Answers (1)

LukeH
LukeH

Reputation: 269428

Don't do anything.

The GC will handle it all for you automatically, collecting any unused vertices and polygons at some point once they're no longer referenced.

Upvotes: 4

Related Questions