.net - are destructors necessary in C#? -


i have concern. i'm first year student of computer science. i'm inquisitive in class but, not teacher has answer, or not knows answer. destructors necessary in c#? mean if have implement destructor method constructors, practice or can avoid , garbage collector me?

destructors (or finalizers) have in language - should never use them. should need them if have direct handle on unmanaged resource, , not incredibly rare, using safehandle tiny level of indirection better idea anyway (which handles clean-up you). see joe duffy's blog post on topic more details.

for it's worth, can't remember last time wrote finalizer other test odd behaviour or other.

for vast majority of time, life simpler:

  • the garbage collector can handle memory resource cleanup
  • if use unmanaged resource (e.g. file) locally within method, use using statement make sure release when you're done it
  • if need reference unmanaged resource (or else implements idisposable) instance variable within type, type should implement idisposable. (i try avoid possible. when is necessary, can make life simpler making class sealed, @ point @ least don't need worry other subclasses having more unmanaged state clean up.)

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -