I know the difference in meaning and use of destructors and finalisers in c#.
However, typically the answer to a "should I ..." is answered by "don't use destructors, rather use the dispose pattern as shown in MSDN". Eric Lippert writes quite strongly against using destructors unnecessarily.
But, that "pattern" advocates writing a destructor as such ~T() { Dispose(false); }
. The stated reason is that it is a "fallback" which is called in case the programmer forgets to call Dispose()
. Of course this ignores the fact that finalisers are indeterminate in their operation, and may never even run.
Hence:
If I use the dispose pattern, should I also provide the destructor? Incidentally, I am only disposing managed resources (the Entity Framework
DataContext
for example).If I do provide a destructor: if my class is derived from an
IDisposable
, which may already provide a destructor, then should I provide one too? I thought that one never writes a destructor in such a case, however docs say that it will call the base class' destructor automatically anyway.