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 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.