They are deleted when the garbage collector starts and determines that the object is no longer in use. Itβs better to dispose of the objects themselves, so resources are immediately freed.
Also consider using the using statement:
using (Pen pen1 = new Pen(pencolour, penSize)) { g.DrawEllipse(pen1, eX, eY, 1, 1); }
This automatically removes the Pen, even if DrawEllipse throws an exception, and the IDE ensures that pen1 is accessible only from the usage block.
source share