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?
That is destructor. It takes care that all resources are released upon garbage collection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With