Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

c#

tilde

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.

like image 363
Keith Sirmons Avatar asked Oct 09 '08 19:10

Keith Sirmons


People also ask

What does tilde mean in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What does the tilde operator do in C?

Tilde operator (~) also called bitwise NOT operator, performs one's complement of any binary number as argument. If the operand to NOT is decimal number then it convert it as binary and perform's one's complement operation.

What does the tilde before a constructor mean in C++?

This is a destructor. It's called when the object is destroyed (out of life scope or deleted).

What is a tilde symbol?

A tilde is a typographical symbol that resembles a wavy line (~). In English, it has no accepted usage in formal writing, but it may occasionally be used for a few different reasons in informal writing. This symbol is also used in math, computer programming, and to form certain letters in Spanish and Portuguese.


1 Answers

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

like image 76
Patrick Desjardins Avatar answered Sep 23 '22 15:09

Patrick Desjardins