Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the tilde (~) character do here [duplicate]

Tags:

c#

tilde

Possible Duplicate:
What does the tilde (~) mean in C#?

class ResourceWrapper
{
    int handle = 0;
    public ResourceWrapper()
    {
        handle = GetWindowsResource();
    }
    ~ResourceWrapper()                     //this line here
    {
        FreeWindowsResource(handle);
        handle = 0;
    }
    [DllImport("dll.dll")]
    static extern int GetWindowsResource();
    [DllImport("dll.dll")]
    static extern void FreeWindowsResource(int handle);
}

What does the tilde do on the line indicated.

I thought that it was the bitwise NOT operator, infact I don't really understand that whole block there, (the commented line and the parentheses blovk after it), it isn't a methd, or a parameter or anything, what is it and why is there a tilde before it?

like image 525
Tom Avatar asked Aug 26 '09 00:08

Tom


Video Answer


2 Answers

That is destructor. It takes care that all resources are released upon garbage collection.

like image 108
Josip Medved Avatar answered Sep 28 '22 05:09

Josip Medved


This implements the finalizer (the Finalize method) of the class. Normally you should not implement a finalizer.

E.g. do this for classes that hold external unmanaged resources but be sure to implement the IDisposable Pattern in this case too.

like image 42
Mischa Avatar answered Sep 28 '22 06:09

Mischa