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:

#include <stdio.h>class Destruct{public:    Destruct() {        printf("Constructing\n");    }    ~Destruct() {        printf("Destructing\n");    }};void Stack() {    printf("Stack - begin\n");    Destruct d;    printf("Stack - end\n");}void Heap() {    printf("Heap - begin\n");    Destruct ...

Get C# 5.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.