tags:

views:

299

answers:

6
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?

+8  A: 

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

Josip Medved
@yshuditelu: Thanks for correction.
Josip Medved
Actually, yshuditelu is wrong, and `~ClassName` _is_ finalizer (i.e. `Object.Finalize()`) - if you use `ildasm` to see what `~ClassName` gets compiled to, you'll see that it becomes `override Finalize`. The reason for the special name is that there's a bit of magic there compared to a simple override - C# compiler always inserts a call to `base.Finalize()`.
Pavel Minaev
@Pavel: While internally it does call base.Finalize() method, it is called destructor (C# programming guide: http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx).
Josip Medved
+2  A: 

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.

Mischa
A: 

Ack, i just found the answer and can't see how to delete my question. it specifies the destructor of the class

Tom
questions at stackoverflow should not be deleted (i don't know whether they can be) ... someone else might have the same question, and find yours (assuming that everyone uses search before asking questions ... otherwise they will be closed as duplicate of this question)
back2dos
A: 

i have no clue about C#, but from what the code does, this looks like a deconstructor, saying

  1. free the resource referenced by handle
  2. set handle to 0 to be sure

would kind of go together with the notion of "not" as well ... :)

i may be wrong though ...

back2dos
just out of curiosity: why did this get voted down? all other answers indicate my guess was right ...
back2dos
I didn't vote it up or down, but at a guess, it could be because you're clearly just guessing (making it hard to trust the answer even if it is correct), or because you got the name wrong. It is destructor, not deconstructor.Both seem minor complaints to me, but some might find them worth the -1
jalf
strange philosophy ... in fact it is evident from looking at the code, but i wanted to emphasize the fact i cannot be 100% sure, because i don't really know C# ... does this mean, that i should downvote people from other platforms/languages applying their general knowledge to my field of expertise (quite often being very far from the right answer, although not even admitting that they are not familiar with the platform (very popular among .NET and java devs)), without even caring to point out why they were wrong? anyway, thanks for your effort ... ;)
back2dos
+2  A: 

Like in C++, ~ClassName is the destructor method. It gets called in C# when the object in question gets cleaned up by the garbage collector. Unlike in C++ where there are deterministic times when the destructor is called, there is no way of predicting when it will be called (or even if it will be called) in C#.

What you are probably looking for is the IDisposable pattern, which provides a much better approach to this.

Matthew Scharley
+1  A: 

That is a Destructor. It gives you implicit control over releasing resources. That is, it is called by the Garbage Collector. If you want explicit control over the release of resources, you can implement IDisposable Check out Implementing Finalize and Dispose to Clean Up Unmanaged Resources. The Framework Design Guidelines also has more information.

JP Alioto