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 usually 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, discussed in depth in Chapter 12, “Constructors and Finalizers.” To put it simply, ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.