-5

I found many article and forms that define the difference b/w finalize and dispose method but still now i have one doubts that is. Doubt : we know that at run time destructor of class convert into finalize method. What will happen if we doesn't define a destructor in Class. I mean till now destructor will convert into finalize or not? . How Memory Management will perform now.

pankaj choudhary
  • 177
  • 1
  • 1
  • 7
  • 1
    "we know that at run time destructor of class convert into finalize method" - do you have any links explaining that? (Since it happens at compile time I don't think you can find one...) – Alexei Levenkov Oct 16 '15 at 17:27
  • Since you saying you've seen many articles already (probably including suggested duplicate), can you also clarify what exactly you trying to understand? Maybe small class like `class MyClass{ public int Data {get;set;}}` without destructor and explanation what you expect/don't expect to happen with such class? – Alexei Levenkov Oct 16 '15 at 17:30

1 Answers1

1

A Dispose method is present on classes that implement IDisposable (I guess you could have one without the interface, but it would be less useful). In such a method you should clean up any un-managed resources you have. It also:

  1. Lets you put a using block around your object so the framework will invoke Dispose for you.
  2. Lets users know they should probably call Dispose before letting the object fall out of scope.

A finalizer (which happens to look like a C++ destructor) is called by the garbage collector... eventually. Given the lack of control you have over when it is invoked, you should be very careful about relying on them. Not having one is no trouble at all (and the vast majority, almost all really, of classes do not need them). Additionally, you definitely don't need one if you implement IDisposable as unmanaged resources should already have been released (and GC.SuppressFinalize called) before the finalizer would run.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Typically, a class that implements `IDisposable` has no use at all for a destructor. It's always recommended to call `GC.SuppressFinalize(this)` on disposing so the garbage collector does not attempt to call the finalizer (and possibly release resources that had already been released) – Matias Cicero Oct 16 '15 at 17:16
  • 1
    @MatiCicero Thanks, Included that. – BradleyDotNET Oct 16 '15 at 17:42