CHAPTER 13 UNDERSTANDING OBJECT LIFETIME
This chapter also illustrated how to programmatically interact with the garbage collector using the
System.GC class type. As mentioned, the only time you will really need to do so is when you are building
finalizable or disposable class types that operate on unmanaged resources.
Recall that finalizable types are classes that have provided a destructor (effectively overriding the
Finalize() method) to clean up unmanaged resources at the time of garbage collection. Disposable
objects, on the other hand, are classes (or structures) that implement the IDisposable interface, which
should be called by the object user when it is finished using said objects. Finally, you learned about an
official “disposal” pattern that blends both approaches.
We wrapped up this chapter with a look at a generic class named Lazy<>. As you have seen, you can
use this class to delay the creation of an expensive (in terms of memory consumption) object until the
caller actually requires it. By doing so, you can help reduce the number of objects stored on the managed
heap and also ensure expensive objects are created only when actually required by the caller.
500