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

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


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

What does the tilde do in C#? [duplicate]

Possible Duplicate: What does the tilde before a function name mean in C#? What is the tilde (~) in the enum definition? I know ~ is for Finalzier methods but now I saw some C# code like this: if

What does this tilde mean?

I was reading through some JMockit examples and found this code: final List<OrderItem> actualItems = new ArrayList<~>(); What does the tilde in the generic identifier mean? I know it’s th

In c++ what does a tilde “~” before a function name signify?

template <class T> class Stack { public: Stack(int = 10) ; ~Stack() { delete [] stackPtr ; } //<— What does the ~ signify? int push(const T&); int pop(T&) ; int isEmpty()const { r

What does a period with a name before a function mean when calling it in Arduino code (C/C++)?

What does a period with a name before a function mean when calling it in Arduino code (C/C++)? For example, I am using an OLED display library and one function is called like this: display.setTextSize

what does tilde mean?

a ~= b*c; It can’t be boolean logic, so what’s that?

What does the tilde (~) in macros mean?

Seen on this site, the code shows macro invocations using a tilde in parentheses: HAS_COMMA(_TRIGGER_PARENTHESIS_ __VA_ARGS__ (~)) // ^^^ What does it mean / do? I suspect it to just be an empty argu

What does the tilde mean in an expression? [duplicate]

Possible Duplicate: What is the tilde (~) in a C# enumeration? I found the following bit of code on a this MSDN page. (((Width * Planes * BitCount + 31) & ~31) / 8) * abs(Height) This does inde

What does this mean ~method( ){ } [duplicate]

Possible Duplicate: What does the tilde (~) mean in C#? What is the meaing of ~ before a method? I saw this like here: ~myDirect3dClass() { }

What does an asterisk before a function name mean?

In the following lines of code, what does the asterisk in front of dup_func, free_func, and clear_free func, do? void *(*dup_func)(void *); void (*free_func)(void *); void (*clear_free_func)(void *);

In C, what does it mean when I declare a variable following a function signature, before the function body? [duplicate]

Possible Duplicate: Why are declarations put between func() and {}? In C, what does it mean when I declare a variable following a function signature, before the function body? Example: int foo (i) i


It is used to indicate the destructor for the class.

Same as C++, it’s the destructor; however in C# you don’t called it explicitely, it is invoked when the object gets collected.

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

Here is a description of C# deconstructors i found

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.

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


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.


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.

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.

One point on the Finalizer above, on the situation you may need to call it. You need them to release unmanaged resources, which are more common than you’d think, usually for databases. SQLConnection is an example on which you should always call Dispose() when you’re done with it.

One article: http://www.codeproject.com/KB/cs/idisposable.aspx