187

I am looking at some code and it has this statement:

~ConnectionManager()
{
    Dispose(false);
}

The class implements the IDisposable interface, but I do not know if that is part of that the tilde(~) is used for.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Keith Sirmons
  • 8,271
  • 15
  • 52
  • 75

6 Answers6

237

~ is the destructor

  1. Destructors are invoked automatically, and cannot be invoked explicitly.
  2. Destructors cannot be overloaded. Thus, a class can have, at most, one destructor.
  3. Destructors are not inherited. Thus, a class has no destructors other than the one, which may be declared in it.
  4. Destructors cannot be used with structs. They are only used with classes. An instance becomes eligible for destruction when it is no longer possible for any code to use the instance.
  5. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
  6. When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.

Finalize

In C#, the Finalize method performs the operations that a standard C++ destructor would do. In C#, you don't name it Finalize -- you use the C++ destructor syntax of placing a tilde ( ~ ) symbol before the name of the class.

Dispose

It is preferable to dispose of objects in a Close() or Dispose() method that can be called explicitly by the user of the class. Finalize (destructor) are called by the GC.

The IDisposable interface tells the world that your class holds onto resources that need to be disposed and provides users a way to release them. If you do need to implement a finalizer in your class, your Dispose method should use the GC.SuppressFinalize() method to ensure that finalization of your instance is suppressed.

What to use?

It is not legal to call a destructor explicitly. Your destructor will be called by the garbage collector. If you do handle precious unmanaged resources (such as file handles) that you want to close and dispose of as quickly as possible, you ought to implement the IDisposable interface.

PedroC88
  • 3,708
  • 7
  • 43
  • 77
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
  • 3
    I don't know how it used to be in the past. But now destructors are inherited. Check this link for more information (check the example at the end): http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx – RononDex Jan 16 '14 at 14:33
  • 1
    @RononDex point 3 of the very page you link to states *"Finalizers cannot be inherited"*, which contradicts your comment. It's admittedly all a bit confusing because while they can't be inherited, *"the `Finalize` method is called recursively for all instances in the inheritance chain, from the most-derived to the least-derived"*. Note that that's *not* the same behaviour you'd get from inheritable destructors, though. – Mark Amery Oct 09 '17 at 20:03
  • @MarkAmery ah I see, so basically they get called in the reversed order compared to something that is inherited? – RononDex Oct 10 '17 at 08:46
  • So if some C# code that is the result of a decompiler has a method like this, it can safely be deleted? This seems to be confirmed by the fact that it has 0 references – Jacob Stamm Jan 25 '22 at 02:26
46

This is a finalizer. To be honest, you should very rarely need to write a finalizer. You really only need to write one if:

  • You have direct access to an unmanaged resource (e.g. through an IntPtr) and you can't use SafeHandle which makes it easier
  • You are implementing IDisposable in a class which isn't sealed. (My preference is to seal classes unless they're designed for inheritance.) A finalizer is part of the canonical Dispose pattern in such cases.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

It is used to indicate the destructor for the class.

torial
  • 13,085
  • 9
  • 62
  • 89
  • 12
    They're both correct, depending on which C# spec you read. The most recent MS one (unified C# 3.0) refers to them as destructors (e.g. section 10.13) but the ECMA spec refers to them as finalizers. – Jon Skeet Oct 09 '08 at 19:10
  • @1800INFORMATION: The *syntactic element* is properly called a destructor. If a class has a destructor, a C# compiler will auto-generate a finalizer which includes a generally-useless `try/finally` block that ensures that the parent `Finalize` method gets called. Most things that are true of destructors are true of finalizers, and vice versa, but the words mean slightly different things. – supercat Jan 16 '14 at 18:05
5

Same as C++, it is the destructor; however in C# you do not call it explicitly, it is invoked when the object gets collected by the garbage collector.

Issung
  • 385
  • 3
  • 14
Santiago Palladino
  • 3,522
  • 2
  • 26
  • 36
3

See Destructors (C# Programming Guide). Be aware, however that, unlike C++, programmer has no control over when the destructor is called because this is determined by the garbage collector.

Jeff Stong
  • 1,506
  • 4
  • 14
  • 26
0

~ usually represents a deconstructor. which is run right before a object dies.

Here is a description of C# deconstructors i found

MichaB
  • 495
  • 7
  • 20
stephenbayer
  • 12,373
  • 15
  • 63
  • 98