Implementing IDisposable
You haven’t seen how to declare a type or implement an interface just yet, but it is very simple to understand. To create an IDisposable
type, you write something like this:
class MyResource : IDisposable /* this means we're implementing IDisposable */{ // Useful members go here. public void Dispose() { // Do some disposal operation here. }}
Things are a bit more complicated, though, because you typically want the underlying resources to get deallocated even if the Dispose
method is not called (because of a type’s user screwing up, for example). This is where the use of a finalizer comes in. We detail the concept of a finalizer when talking about classes, but put simply, it gets called by the garbage ...
Get C# 4.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.