1

In doing a code review I came across this:

public class ClassTilda {
    ~ClassTilda(){
            //code
    }
}

Why is this building and what is that tilda? Can you provide a reference?

sammarcow
  • 2,736
  • 2
  • 28
  • 56

1 Answers1

2

This is Destructor, which is basically not suggested to use in C#.

Destructors are used to destruct instances of classes.

Like was mantioned in comments: there are still cases when you would like to manage, but in most cases it's avoidable as you have:

  • IDisposable when dispose is called on instance of your object interface

  • Finalize() when GC is going to clean your type, so called by GC itself.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 5
    I disagree. It has its uses. Especially when doing Interop. – Anemoia Jan 16 '14 at 16:48
  • Actually you _had_ to use this syntax before Framework 2.0. Now it have been replaced by the Finalize method. – Pierre-Luc Pineault Jan 16 '14 at 16:48
  • @Snake: I didn't say d not use it, it's just not suggested. There *are* naturally cases when they come useful. – Tigran Jan 16 '14 at 16:49
  • @Pierre-LucPineault In what way has `Finalize` replaced it, when you write C# code. I guess you _must_ use the tilde `~` syntax in C#, not the `Finalize` identifier. – Jeppe Stig Nielsen Jan 16 '14 at 16:54
  • @JeppeStigNielsen The destructor syntax is only a syntactic sugar for the Finalize method. And it's only a syntax, because it does not behave like a real destructor, it really is a finalizer. It will only get called at a later time when the object is GCed, not as soon as the object is deleted like in C++. – Pierre-Luc Pineault Jan 16 '14 at 16:59
  • @Pierre-LucPineault Agree, using the tilde `~` syntax will really override the the `Finalize` method. However, it is illegal to use the `Finalize` method directly, in C#. I agree a C# finalizer with `~` is not a destructor in the C++ sense. But it is also not a "usual" method. If a finalizer is present, even an empty one, the garbage collector (GC) will not collect your instance immediately. It will first put it in a queue for finalization, which involves promoting your object to the next GC generation. That is why it can be problematic to have a finalizer. – Jeppe Stig Nielsen Jan 16 '14 at 17:05
  • For the reasons I just mentioned, always remember to call `GC.SuppressFinalize(this)` if your class has a destructor/finalizer but you happen to know that the object is already "cleaned up" (so your destructor code would not do anything). That prevent this instance from being promoted one GC generation, when it becomes garbage. – Jeppe Stig Nielsen Jan 16 '14 at 17:08
  • +Jeppe Stig Nielsen: but you can cleanup the resources you allocated on some "event" that occured in program (if someone calls Dispose(..) on your type, your type is _not_ GC collected, so it's not destructed yet). It's more or less like using smart_pointer in C++, you let environment to call you, and you run your code then. Destructor by itself, in C#, does do anything different then Finalizer, in fact often they called the same thing. Probabbly was introduced by MS to manifest dramatic difference between C# destructor and C++ destructor. – Tigran Jan 17 '14 at 11:51