Proper use of the IDisposable interface
Proper use of the IDisposable interface
Proper use of the IDisposable interface
Re: Proper use of the IDisposable interface
The point of Dispose **is** to free unmanaged resources. It needs to be done at some point, otherwise they will never be cleaned up. The garbage collector doesn't know **how** to call `DeleteHandle()` on a variable of type `IntPtr`, it doesn't know **whether** or not it needs to call `DeleteHandle()`.
**Note**: What is an *unmanaged resource*? If you found it in the Microsoft .NET Framework: it's managed. If you went poking around MSDN yourself, it's unmanaged. Anything you've used P/Invoke calls to get outside of the nice comfy world of everything available to you in the .NET Framework is unmanaged – and you're now responsible for cleaning it up.
The object that you've created needs to expose *some* method, that the outside world can call, in order to clean up unmanaged resources. The method can be named whatever you like:
```
public void Cleanup()
```
or
```
public void Shutdown()
```
But instead there is a standardized name for this method:
```
public void Dispose()
```
There was even an interface created, `IDisposable`, that has just that one method:
```
public interface IDisposable
{
void Dispose();
}
```
So you make your object expose the `IDisposable` interface, and that way you promise that you've written that single method to clean up your unmanaged resources:
```
public void Dispose()
{
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle);
}
```
And you're done.
Except you can do better
What if your object has allocated a 250MB **[System.Drawing.Bitmap](http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx)** (i.e. the .NET managed Bitmap class) as some sort of frame buffer? Sure, this is a managed .NET object, and the garbage collector will free it. But do you really want to leave 250MB of memory just sitting there – waiting for the garbage collector to *eventually* come along and free it? What if there's an [open database connection](http://msdn.microsoft.com/en-us/library/system.data.common.dbconnection.aspx)
*(Réponse tronquée)*
**Note**: What is an *unmanaged resource*? If you found it in the Microsoft .NET Framework: it's managed. If you went poking around MSDN yourself, it's unmanaged. Anything you've used P/Invoke calls to get outside of the nice comfy world of everything available to you in the .NET Framework is unmanaged – and you're now responsible for cleaning it up.
The object that you've created needs to expose *some* method, that the outside world can call, in order to clean up unmanaged resources. The method can be named whatever you like:
```
public void Cleanup()
```
or
```
public void Shutdown()
```
But instead there is a standardized name for this method:
```
public void Dispose()
```
There was even an interface created, `IDisposable`, that has just that one method:
```
public interface IDisposable
{
void Dispose();
}
```
So you make your object expose the `IDisposable` interface, and that way you promise that you've written that single method to clean up your unmanaged resources:
```
public void Dispose()
{
Win32.DestroyHandle(this.CursorFileBitmapIconServiceHandle);
}
```
And you're done.
Except you can do better
What if your object has allocated a 250MB **[System.Drawing.Bitmap](http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx)** (i.e. the .NET managed Bitmap class) as some sort of frame buffer? Sure, this is a managed .NET object, and the garbage collector will free it. But do you really want to leave 250MB of memory just sitting there – waiting for the garbage collector to *eventually* come along and free it? What if there's an [open database connection](http://msdn.microsoft.com/en-us/library/system.data.common.dbconnection.aspx)
*(Réponse tronquée)*