15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

freeing Unmanaged resources ❘ 313<br />

encapsulate direct or indirect references to unmanaged resources, you need to make special provisions to<br />

ensure that the unmanaged resources are released when an instance of the class is garbage collected.<br />

When defi ning a class, you can use two mechanisms to automate the freeing of unmanaged resources. These<br />

mechanisms are often implemented together because each provides a slightly different approach to the<br />

solution of the problem. The mechanisms are:<br />

➤<br />

➤<br />

Declaring a destructor (or fi nalizer) as a member of your class<br />

Implementing the System.IDisposable interface in your class<br />

The following sections discuss each of these mechanisms in turn, <strong>and</strong> then look at how to implement them<br />

together for best effect.<br />

destructors<br />

You have seen that constructors allow you to specify actions that must take place whenever an instance of<br />

a class is created. Conversely, destructors are called before an object is destroyed by the garbage collector.<br />

Given this behavior, a destructor would initially seem like a great place to put code to free unmanaged<br />

resources <strong>and</strong> perform a general cleanup. Unfortunately, things are not so straightforward.<br />

Although we talk about destructors in <strong>C#</strong>, in the underlying .<strong>NET</strong> architecture these<br />

are known as fi n a l i z er s . When you defi ne a destructor in <strong>C#</strong>, what is emitted into the<br />

assembly by the compiler is actually a Finalize() method. It doesn’t affect any of<br />

your source code, but you need to be aware of it when examining the content of an<br />

assembly.<br />

The syntax for a destructor will be familiar to C++ developers. It looks like a method, with the same name<br />

as the containing class, but prefi xed with a tilde ( ~ ). It has no return type, <strong>and</strong> takes no parameters <strong>and</strong> no<br />

access modifi ers. Here is an example:<br />

class MyClass<br />

{<br />

~MyClass()<br />

{<br />

// destructor implementation<br />

}<br />

}<br />

When the <strong>C#</strong> compiler compiles a destructor, it implicitly translates the destructor code to the equivalent<br />

of a Finalize() method, which ensures that the Finalize() method of the parent class is executed. The<br />

following example shows the <strong>C#</strong> code equivalent to the Intermediate Language (IL) that the compiler would<br />

generate for the ~MyClass destructor:<br />

protected override void Finalize()<br />

{<br />

try<br />

{<br />

// destructor implementation<br />

}<br />

finally<br />

{<br />

base.Finalize();<br />

}<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!