Disposal Before Collection: IDisposable

Finalizers are typically implemented to ensure resources that are beyond the garbage collector’s domain of responsibility get cleaned up properly. A good example is when you must deal with unmanaged resources (for example, in Win32 interop scenarios requiring manual release of object using calls to CloseHandle). Although lots of Win32 APIs have been wrapped in the .NET Framework, you might sometimes need to P/Invoke some other APIs manually. As an example, consider the CreateJobObject Win32 function, which allows for the creation of jobs, which are groups of processes. A managed code wrapper could look like this:

Don’t worry too much about the P/Invoke signatures, which are a subject on their own. Adam ...

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.