0

Let's assume there is a class like this:

public class NewClass : IDisposable
{
  public string name {get; set;}
  public NewClass(string Name)
  {
    name = Name;
  }

  ~NewClass()
  {
    Dispose(false);
  }

   public void Dispose()
   {
    Dispose(true);
    System.GC.SuppressFinalize(this);
   }
}
  • [Finalizers](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/destructors) – Pikoh Oct 04 '17 at 11:16
  • Or destructors? Looks like even Microsoft is not sure. – IS4 Oct 04 '17 at 11:19
  • @IllidanS4 if you look at the link, it contains `destructors`, but the document talks about finalizers – Pikoh Oct 04 '17 at 11:22
  • because there's a mystery surrounding this question and the usage of '~' can someone please explain how one can use such Finalizer/Destructor successfully? There is no Stack Overflow answer to help explain that.... –  Oct 04 '17 at 11:23
  • @Pikoh That's precisely what I did and pointed out. – IS4 Oct 04 '17 at 11:23
  • @IllidanS4 [related](https://stackoverflow.com/q/1076965/579895) – Pikoh Oct 04 '17 at 11:24
  • @fubo can you please double check the question, which I have asked, as I can not find the exact one on Stack Overflow? –  Oct 04 '17 at 11:25
  • Finalizers (or destructors) are somehow similar to destructors in c++, with a fundamental difference: you cannot now when they will be called. The garbage collector will (eventually) call it when releasing object memory, which is something you cannot (or better shouldn't) control from your code. The one, and maybe only, reason to implement them is it to correctly call Dispose (for classes implementing IDisposable): have a look at [this](https://stackoverflow.com/a/31016954/3276027) – Gian Paolo Oct 04 '17 at 11:35

0 Answers0