What does the tilde before a function name mean in C#?

C#SyntaxTilde

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

~ 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.

Solution 2 - C#

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.

Solution 3 - C#

It is used to indicate the destructor for the class.

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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

Here is a description of C# deconstructors i found

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKeith SirmonsView Question on Stackoverflow
Solution 1 - C#Patrick DesjardinsView Answer on Stackoverflow
Solution 2 - C#Jon SkeetView Answer on Stackoverflow
Solution 3 - C#torialView Answer on Stackoverflow
Solution 4 - C#Santiago PalladinoView Answer on Stackoverflow
Solution 5 - C#Jeff StongView Answer on Stackoverflow
Solution 6 - C#stephenbayerView Answer on Stackoverflow