O'Reilly logo

Applied Microsoft® .NET Framework Programming by Jeffrey Richter

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Dispose Pattern: Forcing an Object to Clean Up

The Finalize method is incredibly useful because it ensures that unmanaged resources aren’t leaked when managed objects have their memory reclaimed. However, the problem with the Finalize method is that you have no guarantee when it will be called, and because it isn’t a public method, a user of the class can’t call it explicitly.

The capability to deterministically dispose of or close an object is frequently useful when you’re working with unmanaged resources such as mutexes and bitmaps; this capability is even more useful when you’re working with files and database connections. For example, you might want to open a database connection, query some records, and close the database connection—you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required