Destructors (Poorly Named Finalizers)

Besides constructors, C# has the notion of destructors. Before going any further, let’s start with the mandatory warning. In the world of garbage collection, destructors only share their name with the concept known from C++. That is, they are totally different in behavior, despite their common syntax. To put this straight, consider the following piece of C++ code. Readers unfamiliar with C++ can skip the following example.

image

One key difference is the fact that C++ classes and structs can be allocated either on the stack or the heap, depending on their use. Just declaring a local variable of the type will cause ...

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.